Commit 7583a8d8 authored by romanb's avatar romanb

Preliminary fix for a bug that occurs when using NestedSet + Column aggregation inheritance

parent 15e43b61
......@@ -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()));
......
......@@ -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;
}
}
......@@ -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;
}
......
......@@ -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']);
}
......
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