/** * @depends test_main * query one-to-one relationships */ public function test_one_to_one() { if ($this->_need_skip_test(__FUNCTION__)) { return; } $bear_lawly = bear::where('name', '=', 'Lawly')->first(); $bear_adobot = bear::where('name', '=', 'Adobot')->first(); // find a bear named Adobot $adobot = bear::where('name', '=', 'Adobot')->first(); // get the fish that Adobot has $fish = $adobot->fish; // get the weight of the fish Adobot is going to eat $weight1 = $fish->weight; // alternatively you could go straight to the weight attribute $weight2 = $adobot->fish->weight; $this->assertInternalType('object', $adobot); $this->assertNotSame($bear_adobot, $adobot); $this->assertEquals($bear_adobot->id, $adobot->id); $this->assertEquals($bear_adobot->name, $bear_adobot->name); $this->assertInternalType('object', $adobot->fish); $this->assertInternalType('object', $fish); $this->assertNotSame($fish, $adobot->fish); $this->assertEquals($fish, $adobot->fish); $this->assertInstanceOf('yf_model_result', $adobot->fish); $this->assertInstanceOf('yf_model_result', $fish); $this->assertInstanceOf('yf_model', $fish->_get_model()); $this->assertInstanceOf('fish', $fish->_get_model()); // Find bear that holds the fish1 $fish_first = fish::find(1); $bear_fish1 = $fish_first->bear; $this->assertInternalType('object', $fish_first); $this->assertInstanceOf('yf_model_result', $fish_first); $this->assertInstanceOf('yf_model', $fish_first->_get_model()); $this->assertInstanceOf('fish', $fish_first->_get_model()); $this->assertNotSame($fish1, $fish_first); $this->assertInternalType('object', $bear_fish1); $this->assertInstanceOf('yf_model_result', $bear_fish1); $this->assertInstanceOf('yf_model', $bear_fish1->_get_model()); $this->assertInstanceOf('bear', $bear_fish1->_get_model()); $this->assertEquals($bear_lawly->id, $bear_fish1->id); $this->assertEquals($bear_lawly->name, $bear_fish1->name); $this->assertEquals($bear_lawly->name, $fish_first->bear->name); }
<?php class fish { function swim($speed) { // critical error, fish could explode if faster if ($speed > 120) { throw new Exception('swim limit reached.'); } } } // new fish $turbofish = new fish(); // try to speedup try { // try to be a turbo-fish $turbofish->swim(121); } catch (Exception $e) { // go deeper, to gain speed echo "Exception caught!\n"; }