Skip to content

Commit

Permalink
Isolating robots relations
Browse files Browse the repository at this point in the history
  • Loading branch information
phalcon committed Jul 7, 2013
1 parent 14d730f commit ba14874
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 70 deletions.
46 changes: 23 additions & 23 deletions unit-tests/ModelsRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,59 +122,59 @@ public function _executeTestsNormal($di)

$manager = $di->getShared('modelsManager');

$success = $manager->existsBelongsTo('RobotsParts', 'Robots');
$success = $manager->existsBelongsTo('RelationsRobotsParts', 'RelationsRobots');
$this->assertTrue($success);

$success = $manager->existsBelongsTo('RobotsParts', 'Parts');
$success = $manager->existsBelongsTo('RelationsRobotsParts', 'RelationsParts');
$this->assertTrue($success);

$success = $manager->existsHasMany('Robots', 'RobotsParts');
$success = $manager->existsHasMany('RelationsRobots', 'RelationsRobotsParts');
$this->assertTrue($success);

$success = $manager->existsHasMany('Parts', 'RobotsParts');
$success = $manager->existsHasMany('RelationsParts', 'RelationsRobotsParts');
$this->assertTrue($success);

$success = $manager->existsHasManyToMany('Robots', 'Parts');
$success = $manager->existsHasManyToMany('RelationsRobots', 'RelationsParts');
$this->assertTrue($success);

$robot = Robots::findFirst();
$robot = RelationsRobots::findFirst();
$this->assertNotEquals($robot, false);

$robotsParts = $robot->getRobotsParts();
$robotsParts = $robot->getRelationsRobotsParts();
$this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($robotsParts), 3);

$parts = $robot->getParts();
$parts = $robot->getRelationsParts();
$this->assertEquals(get_class($parts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($parts), 3);

/** Passing parameters to magic methods **/
$robotsParts = $robot->getRobotsParts("parts_id = 1");
$robotsParts = $robot->getRelationsRobotsParts("parts_id = 1");
$this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($robotsParts), 1);

/** Passing parameters to magic methods **/
$parts = $robot->getParts("Parts.id = 1");
$parts = $robot->getRelationsParts("RelationsParts.id = 1");
$this->assertEquals(get_class($parts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($parts), 1);

$robotsParts = $robot->getRobotsParts(array(
$robotsParts = $robot->getRelationsRobotsParts(array(
"parts_id > :parts_id:",
"bind" => array("parts_id" => 1)
));
$this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($robotsParts), 2);
$this->assertEquals($robotsParts->getFirst()->parts_id, 2);

$parts = $robot->getParts(array(
"Parts.id > :id:",
$parts = $robot->getRelationsParts(array(
"RelationsParts.id > :id:",
"bind" => array("id" => 1)
));
$this->assertEquals(get_class($parts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($parts), 2);
$this->assertEquals($parts->getFirst()->id, 2);

$robotsParts = $robot->getRobotsParts(array(
$robotsParts = $robot->getRelationsRobotsParts(array(
"parts_id > :parts_id:",
"bind" => array("parts_id" => 1),
"order" => "parts_id DESC"
Expand All @@ -184,27 +184,27 @@ public function _executeTestsNormal($di)
$this->assertEquals($robotsParts->getFirst()->parts_id, 3);

/** Magic counting */
$number = $robot->countRobotsParts();
$number = $robot->countRelationsRobotsParts();
$this->assertEquals($number, 3);

$part = Parts::findFirst();
$part = RelationsParts::findFirst();
$this->assertNotEquals($part, false);

$robotsParts = $part->getRobotsParts();
$robotsParts = $part->getRelationsRobotsParts();
$this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple');
$this->assertEquals(count($robotsParts), 1);

$number = $part->countRobotsParts();
$number = $part->countRelationsRobotsParts();
$this->assertEquals($number, 1);

$robotPart = RobotsParts::findFirst();
$robotPart = RelationsRobotsParts::findFirst();
$this->assertNotEquals($robotPart, false);

$robot = $robotPart->getRobots();
$this->assertEquals(get_class($robot), 'Robots');
$robot = $robotPart->getRelationsRobots();
$this->assertEquals(get_class($robot), 'RelationsRobots');

$part = $robotPart->getParts();
$this->assertEquals(get_class($part), 'Parts');
$part = $robotPart->getRelationsParts();
$this->assertEquals(get_class($part), 'RelationsParts');

/** Relations in namespaced models */
$robot = Some\Robots::findFirst();
Expand Down
15 changes: 0 additions & 15 deletions unit-tests/models/Relations/Parts.php

This file was deleted.

20 changes: 20 additions & 0 deletions unit-tests/models/Relations/RelationsParts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class RelationsParts extends Phalcon\Mvc\Model
{

public function initialize()
{
$this->hasMany('id', 'RelationsRobotsParts', 'parts_id', array(
'foreignKey' => array(
'message' => 'Parts cannot be deleted because is referenced by a Robot'
)
));
}

public function getSource()
{
return 'parts';
}

}
19 changes: 19 additions & 0 deletions unit-tests/models/Relations/RelationsRobots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class RelationsRobots extends Phalcon\Mvc\Model
{

public function initialize()
{
$this->hasMany('id', 'RelationsRobotsParts', 'robots_id', array(
'foreignKey' => true
));
$this->hasManyToMany('id', 'RelationsRobotsParts', 'robots_id', 'parts_id', 'RelationsParts', 'id');
}

public function getSource()
{
return 'robots';
}

}
23 changes: 23 additions & 0 deletions unit-tests/models/Relations/RelationsRobotsParts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class RelationsRobotsParts extends Phalcon\Mvc\Model
{

public function initialize()
{
$this->belongsTo('parts_id', 'RelationsParts', 'id', array(
'foreignKey' => true
));
$this->belongsTo('robots_id', 'RelationsRobots', 'id', array(
'foreignKey' => array(
'message' => 'The robot code does not exist'
)
));
}

public function getSource()
{
return 'robots_parts';
}

}
14 changes: 0 additions & 14 deletions unit-tests/models/Relations/Robots.php

This file was deleted.

18 changes: 0 additions & 18 deletions unit-tests/models/Relations/RobotsParts.php

This file was deleted.

0 comments on commit ba14874

Please sign in to comment.