From 83d89b766d884036a7f65a96803a385e918429fd Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 4 Jan 2007 21:08:56 +0000 Subject: [PATCH] TreeStructure bug fixed --- lib/Doctrine/Record.php | 8 +++++--- lib/Doctrine/Relation.php | 6 +++--- lib/Doctrine/Relation/ForeignKey.php | 8 ++++++-- lib/Doctrine/Table.php | 11 +++++++++++ tests/TreeStructureTestCase.php | 26 +++++++++++++++++++++++--- tests/run.php | 4 ++-- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 372e1c80c..67fdec26d 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1315,10 +1315,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ final public function loadReference($name) { + $fk = $this->_table->getRelation($name); if ($fk->isOneToOne()) { $this->references[$name] = $fk->fetchRelatedFor($this); + } else { $coll = $fk->fetchRelatedFor($this); @@ -1344,7 +1346,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @param string $fkField * @return void */ - final public function ownsMany($componentName,$foreignKey, $localKey = null) + final public function ownsMany($componentName, $foreignKey, $localKey = null) { $this->_table->bind($componentName, $foreignKey, Doctrine_Relation::MANY_COMPOSITE, $localKey); } @@ -1355,7 +1357,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @param string $fkField * @return void */ - final public function hasOne($componentName,$foreignKey, $localKey = null) + final public function hasOne($componentName, $foreignKey, $localKey = null) { $this->_table->bind($componentName, $foreignKey, Doctrine_Relation::ONE_AGGREGATE, $localKey); } @@ -1366,7 +1368,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @param string $fkField * @return void */ - final public function hasMany($componentName,$foreignKey, $localKey = null) + final public function hasMany($componentName, $foreignKey, $localKey = null) { $this->_table->bind($componentName, $foreignKey, Doctrine_Relation::MANY_AGGREGATE, $localKey); } diff --git a/lib/Doctrine/Relation.php b/lib/Doctrine/Relation.php index c45d9d6a1..34cb00a91 100644 --- a/lib/Doctrine/Relation.php +++ b/lib/Doctrine/Relation.php @@ -171,9 +171,9 @@ abstract class Doctrine_Relation */ public function getRelationDql($count) { - $dql = "FROM ".$this->table->getComponentName(). - " WHERE ".$this->table->getComponentName(). '.' . $this->foreign. - " IN (".substr(str_repeat("?, ", $count),0,-2).")"; + $dql = 'FROM ' . $this->table->getComponentName() + . ' WHERE ' . $this->table->getComponentName() . '.' . $this->foreign + . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')'; return $dql; } diff --git a/lib/Doctrine/Relation/ForeignKey.php b/lib/Doctrine/Relation/ForeignKey.php index a1b55498c..1d2394b1f 100644 --- a/lib/Doctrine/Relation/ForeignKey.php +++ b/lib/Doctrine/Relation/ForeignKey.php @@ -81,14 +81,18 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation if (empty($id)) { $related = $this->table->create(); } else { - $dql = "FROM ".$this->table->getComponentName()." WHERE ".$this->table->getComponentName().".".$this->foreign." = ?"; + $dql = 'FROM ' . $this->table->getComponentName() + . ' WHERE ' . $this->table->getComponentName() + . '.' . $this->foreign . ' = ?'; + $coll = $this->table->getConnection()->query($dql, array($id)); $related = $coll[0]; } $related->set($this->foreign, $record, false); - } else { + } else { + if (empty($id)) { $related = new Doctrine_Collection($this->table); } else { diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index e4a69dbd0..59092a522 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -699,17 +699,28 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable // ONE-TO-ONE if ($type == Doctrine_Relation::ONE_COMPOSITE || $type == Doctrine_Relation::ONE_AGGREGATE) { + // tree structure parent relation found + if ( ! isset($local)) { $local = $table->getIdentifier(); } $relation = new Doctrine_Relation_LocalKey($table, $foreign, $local, $type, $alias); } else { + // tree structure children relation found + + if ( ! isset($local)) { + $tmp = $table->getIdentifier(); + } + $local = $foreign; + $foreign = $tmp; + $relation = new Doctrine_Relation_ForeignKey($table, $foreign, $local, $type, $alias); } } elseif ($component == $name || ($component == $alias)) { // && ($name == $this->options['name'] || in_array($name,$this->parents)) + if ( ! isset($local)) { $local = $this->identifier; } diff --git a/tests/TreeStructureTestCase.php b/tests/TreeStructureTestCase.php index e464c4440..70a8414aa 100644 --- a/tests/TreeStructureTestCase.php +++ b/tests/TreeStructureTestCase.php @@ -3,8 +3,12 @@ class TreeLeaf extends Doctrine_Record { public function setTableDefinition() { + $this->hasColumn('name', 'string'); $this->hasColumn('parent_id', 'integer'); - $this->hasOne('TreeLeaf as Parent', 'TreeLeaf.parent_id', 'id'); + } + public function setUp() + { + $this->hasOne('TreeLeaf as Parent', 'TreeLeaf.parent_id'); $this->hasMany('TreeLeaf as Children', 'TreeLeaf.parent_id'); } } @@ -27,6 +31,18 @@ class Doctrine_TreeStructure_TestCase extends Doctrine_UnitTestCase } } + public function testLocalAndForeignKeysAreSetCorrectly() { + $component = new TreeLeaf(); + + $rel = $component->getTable()->getRelation('Parent'); + $this->assertEqual($rel->getLocal(), 'parent_id'); + $this->assertEqual($rel->getForeign(), 'id'); + + $rel = $component->getTable()->getRelation('Children'); + $this->assertEqual($rel->getLocal(), 'id'); + $this->assertEqual($rel->getForeign(), 'parent_id'); + } + public function testTreeLeafRelationships() { /* structure: @@ -42,22 +58,26 @@ class Doctrine_TreeStructure_TestCase extends Doctrine_UnitTestCase */ $o1 = new TreeLeaf(); - $o1->Parent = null; + $o1->name = 'o1'; $o1->save(); $o2 = new TreeLeaf(); + $o2->name = 'o2'; $o2->Parent = $o1; $o2->save(); $o3 = new TreeLeaf(); + $o3->name = 'o3'; $o3->Parent = $o1; $o3->save(); - $o1->refresh(); + //$o1->refresh(); $o4 = new TreeLeaf(); + $o4->name = 'o4'; $o4->save(); + $o1->Children; $this->assertFalse(isset($o1->Parent)); $this->assertTrue(count($o1->Children) == 2); $this->assertTrue(count($o1->get('Children')) == 2); diff --git a/tests/run.php b/tests/run.php index dbf8e9a64..f1bb0c257 100644 --- a/tests/run.php +++ b/tests/run.php @@ -136,7 +136,6 @@ $test->addTestCase(new Doctrine_Collection_TestCase()); $test->addTestCase(new Doctrine_Relation_TestCase()); $test->addTestCase(new Doctrine_Relation_Access_TestCase()); $test->addTestCase(new Doctrine_Relation_ManyToMany_TestCase()); -$test->addTestCase(new Doctrine_TreeStructure_TestCase()); // Datatypes @@ -192,9 +191,10 @@ $test->addTestCase(new Doctrine_Query_AggregateValue_TestCase()); $test->addTestCase(new Doctrine_Query_Select_TestCase()); $test->addTestCase(new Doctrine_Query_Expression_TestCase()); $test->addTestCase(new Doctrine_Query_Having_TestCase()); - $test->addTestCase(new Doctrine_Query_JoinCondition_TestCase()); + +$test->addTestCase(new Doctrine_TreeStructure_TestCase()); // Cache tests //$test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());