From 7583a8d8ac083f5b43ee95231a5ac68426ab4790 Mon Sep 17 00:00:00 2001 From: romanb Date: Sun, 29 Jul 2007 19:38:11 +0000 Subject: [PATCH] Preliminary fix for a bug that occurs when using NestedSet + Column aggregation inheritance --- lib/Doctrine/Node/NestedSet.php | 14 +++++++------- lib/Doctrine/Tree.php | 20 ++++++++++++++++++++ lib/Doctrine/Tree/NestedSet.php | 3 +-- tests/NestedSet/SingleRootTestCase.php | 8 ++++++-- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/Node/NestedSet.php b/lib/Doctrine/Node/NestedSet.php index 5e0d036ce..ad484cc51 100644 --- a/lib/Doctrine/Node/NestedSet.php +++ b/lib/Doctrine/Node/NestedSet.php @@ -527,7 +527,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int // Relocate descendants of the node $diff = $this->getLeftValue() - $oldLft; - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $rootColName = $this->record->getTable()->getTree()->getAttribute('rootColumnName'); // Update lft/rgt/root/level for all descendants @@ -652,7 +652,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int // Update descendants lft/rgt/root/level values $diff = 1 - $oldLft; $newRoot = $newRootId; - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $rootColName = $this->record->getTable()->getTree()->getAttribute('rootColumnName'); $q = new Doctrine_Query($conn); $q = $q->update($componentName) @@ -763,7 +763,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int $oldRoot = $this->getRootValue(); $q = $this->_tree->getBaseQuery(); - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $q = $q->addWhere('base.lft >= ? AND base.rgt <= ?', array($this->getLeftValue(), $this->getRightValue())); @@ -802,7 +802,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int */ private function updateNode($destLeft, $levelDiff) { - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $left = $this->getLeftValue(); $right = $this->getRightValue(); $rootId = $this->getRootValue(); @@ -848,7 +848,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int $qRight = new Doctrine_Query(); // shift left columns - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $qLeft = $qLeft->update($componentName) ->set($componentName . '.lft', 'lft + ?', $delta) ->where($componentName . '.lft >= ?', $first); @@ -881,7 +881,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int $qRight = new Doctrine_Query(); // shift left column values - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $qLeft = $qLeft->update($componentName) ->set($componentName . '.lft', 'lft + ?', $delta) ->where($componentName . '.lft >= ? AND ' . $componentName . '.lft <= ?', array($first, $last)); @@ -948,7 +948,7 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int public function getLevel() { if (!isset($this->record['level'])) { - $componentName = $this->record->getTable()->getComponentName(); + $componentName = $this->_tree->getBaseComponent(); $q = $this->_tree->getBaseQuery(); $q = $q->addWhere('base.lft < ? AND base.rgt > ?', array($this->getLeftValue(), $this->getRightValue())); diff --git a/lib/Doctrine/Tree.php b/lib/Doctrine/Tree.php index 9738d7900..482c2d059 100644 --- a/lib/Doctrine/Tree.php +++ b/lib/Doctrine/Tree.php @@ -40,6 +40,8 @@ class Doctrine_Tree * @param array $options */ protected $options = array(); + + protected $_baseComponent; /** * constructor, creates tree with reference to table and any options @@ -51,6 +53,16 @@ class Doctrine_Tree { $this->table = $table; $this->options = $options; + $this->_baseComponent = $table->getComponentName(); + $class = $this->_baseComponent; + if ($table->getOption('inheritanceMap')) { + $subclasses = $table->getOption('subclasses'); + while (in_array($class, $subclasses)) { + $class = get_parent_class($class); + } + $this->_baseComponent = $class; + } + //echo $this->_baseComponent; } /** @@ -107,4 +119,12 @@ class Doctrine_Tree { $this->options[$name] = $value; } + + /** + * Returns the base tree component. + */ + public function getBaseComponent() + { + return $this->_baseComponent; + } } diff --git a/lib/Doctrine/Tree/NestedSet.php b/lib/Doctrine/Tree/NestedSet.php index 0223e473c..f5b872cdc 100644 --- a/lib/Doctrine/Tree/NestedSet.php +++ b/lib/Doctrine/Tree/NestedSet.php @@ -142,7 +142,6 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int { // fetch tree $q = $this->getBaseQuery(); - $componentName = $this->table->getComponentName(); $q = $q->addWhere("base.lft >= ?", 1); @@ -277,7 +276,7 @@ class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Int private function _createBaseQuery() { $q = new Doctrine_Query(); - $q->select("base.*")->from($this->table->getComponentName() . " base"); + $q->select("base.*")->from($this->getBaseComponent() . " base"); return $q; } diff --git a/tests/NestedSet/SingleRootTestCase.php b/tests/NestedSet/SingleRootTestCase.php index 71f93026c..8dcf2efee 100644 --- a/tests/NestedSet/SingleRootTestCase.php +++ b/tests/NestedSet/SingleRootTestCase.php @@ -48,6 +48,10 @@ class Doctrine_NestedSet_SingleRoot_TestCase extends Doctrine_UnitTestCase $node2 = new NestedSetTest_SingleRootNode(); $node2->name = 'node2'; $node2->getNode()->insertAsLastChildOf($node); + + $node3 = new NestedSetTest_SingleRootNode(); + $node3->name = 'node3'; + $node3->getNode()->insertAsLastChildOf($node2); } public function testLftRgtValues() @@ -55,7 +59,7 @@ class Doctrine_NestedSet_SingleRoot_TestCase extends Doctrine_UnitTestCase $treeMngr = $this->conn->getTable('NestedSetTest_SingleRootNode')->getTree(); $root = $treeMngr->fetchRoot(); $this->assertEqual(1, $root['lft']); - $this->assertEqual(4, $root['rgt']); + $this->assertEqual(6, $root['rgt']); } public function testGetDescendants() @@ -64,7 +68,7 @@ class Doctrine_NestedSet_SingleRoot_TestCase extends Doctrine_UnitTestCase $root = $treeMngr->fetchRoot(); $desc = $root->getNode()->getDescendants(); $this->assertTrue($desc !== false); - $this->assertEqual(1, count($desc)); + $this->assertEqual(2, count($desc)); $this->assertEqual('node2', $desc[0]['name']); $this->assertEqual(1, $desc[0]['level']); }