Commit 83d89b76 authored by zYne's avatar zYne

TreeStructure bug fixed

parent b6368617
......@@ -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);
}
......
......@@ -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;
}
......
......@@ -81,7 +81,10 @@ 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];
}
......@@ -89,6 +92,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
$related->set($this->foreign, $record, false);
} else {
if (empty($id)) {
$related = new Doctrine_Collection($this->table);
} else {
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment