public function _executeTestsNormal($di) { //Normal foreign keys $robotsParts = new RobotsParts(); $robotsParts->robots_id = 1; $robotsParts->parts_id = 100; $this->assertFalse($robotsParts->save()); $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'Value of field "parts_id" does not exist on referenced table', '_field' => 'parts_id'))); $this->assertEquals($robotsParts->getMessages(), $messages); $robotsParts->robots_id = 100; $robotsParts->parts_id = 1; $this->assertFalse($robotsParts->save()); $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'The robot code does not exist', '_field' => 'robots_id'))); $this->assertEquals($robotsParts->getMessages(), $messages); //Reverse foreign keys $robot = Robots::findFirst(); $this->assertNotEquals($robot, false); $this->assertFalse($robot->delete()); $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'Record is referenced by model RobotsParts', '_field' => 'id'))); $this->assertEquals($robot->getMessages(), $messages); $part = Parts::findFirst(); $this->assertNotEquals($part, false); $this->assertFalse($part->delete()); $messages = array(0 => Phalcon\Mvc\Model\Message::__set_state(array('_type' => 'ConstraintViolation', '_message' => 'Parts cannot be deleted because is referenced by a Robot', '_field' => 'id'))); $this->assertEquals($part->getMessages(), $messages); }
protected function _executeJoinTests($di, $dbtype) { //Left join with Simple resultset $robotparts = RobotsParts::query()->columns("Robots.id, Robots.name, RobotsParts.id robotpart_id")->leftJoin("Robots", "Robots.id = RobotsParts.robots_id")->execute(); $this->assertTrue(is_object($robotparts)); $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robotparts); $this->assertNotNull($robotparts->getFirst()->id); $this->assertNotNull($robotparts->getFirst()->name); $this->assertNotNull($robotparts->getFirst()->robotpart_id); //Two left joins with Simple resultset $robotparts = RobotsParts::query()->columns("RobotsParts.id, r.id robot_id, p.id part_id")->leftJoin("Robots", "r.id = RobotsParts.robots_id", "r")->leftJoin("Parts", "p.id = RobotsParts.parts_id", "p")->execute(); $this->assertTrue(is_object($robotparts)); $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robotparts); $this->assertNotNull($robotparts->getFirst()->id); $this->assertNotNull($robotparts->getFirst()->robot_id); $this->assertNotNull($robotparts->getFirst()->part_id); //Right join not supported in sqlite if ($dbtype != "sqlite") { //Right join with Simple resultset $robotparts = RobotsParts::query()->columns("Robots.id, Robots.name, RobotsParts.id robotpart_id")->rightJoin("Robots", "Robots.id = RobotsParts.robots_id")->execute(); $this->assertTrue(is_object($robotparts)); $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robotparts); $this->assertNotNull($robotparts->getFirst()->id); $this->assertNotNull($robotparts->getFirst()->name); $this->assertNotNull($robotparts->getFirst()->robotpart_id); //Two right joins with Simple resultset $robotparts = RobotsParts::query()->columns("RobotsParts.id, r.id robot_id, p.id part_id")->rightJoin("Robots", "r.id = RobotsParts.robots_id", "r")->rightJoin("Parts", "p.id = RobotsParts.parts_id", "p")->execute(); $this->assertTrue(is_object($robotparts)); $this->assertInstanceOf('Phalcon\\Mvc\\Model\\Resultset\\Simple', $robotparts); $this->assertNotNull($robotparts->getFirst()->id); $this->assertNotNull($robotparts->getFirst()->robot_id); $this->assertNotNull($robotparts->getFirst()->part_id); } }
<?php $robot = Robots::findFirst(2); // Robots model has a 1-n (hasMany) // relationship to RobotsParts then $robotsParts = $robot->robotsParts; // Only parts that match conditions $robotsParts = $robot->getRobotsParts("created_at = '2012-03-15'"); // Or using bound parameters $robotsParts = $robot->getRobotsParts(array("created_at = :date:", "bind" => array("date" => "2012-03-15"))); $robotPart = RobotsParts::findFirst(1); // RobotsParts model has a n-1 (belongsTo) // relationship to RobotsParts then $robot = $robotPart->robots;