Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.2.1
-
Fix Version/s: 1.2.2
-
Component/s: Nested Set
-
Labels:None
-
Environment:Windows XP, Wampserver 2.0 with php 5.2.9-2 and mysql 5.1.33 and apache 2.2.11
Description
When using the code:
$parent = new Category();
$parent->setName('Parent');
$parent->save();
Doctrine_Core::getTable('Category')->getTree()->createRoot($parent);
$child = new Category();
$child->setName('Child');
$child->save();
$child->getNode()->insertAsLastChildOf($parent);
Now it throws an exception with the message:
"Unknown component alias Category" from the Doctrine_Query_Abstract class, line 743.
Tracking down the problem, it seems that in the method shiftRlValues there was a problem:
// shift left columns
$componentName = $this->_tree->getBaseComponent();
$qLeft = Doctrine_Core::getTable($componentName)
->createQuery()
->update();
$qRight = Doctrine_Core::getTable($componentName)
->createQuery()
->update();
$qLeft = $qLeft->set($componentName . '.lft', $componentName.'.lft + ?', $delta)
->where($componentName . '.lft >= ?', $first);
$qLeft = $this->_tree->returnQueryWithRootId($qLeft, $rootId);
$resultLeft = $qLeft->execute();
// shift right columns
$qRight = $qRight->set($componentName . '.rgt', $componentName.'.rgt + ?', $delta)
->where($componentName . '.rgt >= ?', $first);
$qRight = $this->_tree->returnQueryWithRootId($qRight, $rootId);
$resultRight = $qRight->execute();
Making a comparison with the NestedSet class in the 1.0 series, realized that the update methods need the component name resulting in:
// shift left columns
$componentName = $this->_tree->getBaseComponent();
$qLeft = Doctrine_Core::getTable($componentName)
->createQuery()
->update(<b>$componentName</b>);
$qRight = Doctrine_Core::getTable($componentName)
->createQuery()
->update(<b>$componentName</b>);
$qLeft = $qLeft->set($componentName . '.lft', $componentName.'.lft + ?', $delta)
->where($componentName . '.lft >= ?', $first);
$qLeft = $this->_tree->returnQueryWithRootId($qLeft, $rootId);
$resultLeft = $qLeft->execute();
// shift right columns
$qRight = $qRight->set($componentName . '.rgt', $componentName.'.rgt + ?', $delta)
->where($componentName . '.rgt >= ?', $first);
$qRight = $this->_tree->returnQueryWithRootId($qRight, $rootId);
$resultRight = $qRight->execute();
Now the queries work correctly!.
.............
I seems the cause of the problem was the fact of overwriting createQuery method from Doctrine_Table
public function createQuery($alias = '') { $alias = empty($alias) ? $this->getAlias() : trim($alias); $alias = ' '.$alias; $class = $this->getAttribute(Doctrine_Core::ATTR_QUERY_CLASS); return Doctrine_Query::create(null, $class)->from($this->getComponentName().$alias); } public function getAlias() { $name = substr(get_class($this), 0, -5); $name = sfInflector::underscore($name); $names = explode('_', $name); array_walk($names, create_function('&$v, $k', '$v = $v{0};')); $alias = implode('', $names); return $alias; }
I was trying to give and alias always by default:
Category -> c
CategoryAttribute -> ca
CategoryAttributeExtension -> cae
This breaks shiftRlValues method from NestedSet because there, it is used the component name as alias, and when createQuery creates a "from" part with a different alias the whole query crush.
It would be nice to have this feature in a future release.
Can you provide these changes as a patch? It's hard to identify the changes and understand everything without it.