Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.1.4
-
Fix Version/s: 1.0.13, 1.1.5, 1.2.0-BETA1
-
Component/s: Behaviors
-
Labels:None
-
Environment:Windows
PHP 5.3.0
Description
This is current version of getParent()
public function getParent()
{
$baseAlias = $this->_tree->getBaseAlias();
$q = $this->_tree->getBaseQuery();
$q->addWhere("$baseAlias.lft < ? AND $baseAlias.rgt > ?", array($this->getLeftValue(), $this->getRightValue()))
->addOrderBy("$baseAlias.rgt asc");
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
$result = $q->execute();
if (count($result) <= 0) {
return false;
}
if ($result instanceof Doctrine_Collection) {
$parent = $result->getFirst();
} else if (is_array($result)) {
$parent = array_shift($result);
}
return $parent;
}
As you can see, this method does not have level limit in where clause. If tree have more than 2 levels in depth, method will return root node of the tree, not parent.
My fix is use level limit:
public function getParent()
{
$baseAlias = $this->_tree->getBaseAlias();
$q = $this->_tree->getBaseQuery();
$q->addWhere("$baseAlias.lft < ? AND $baseAlias.rgt > ?", array($this->getLeftValue(), $this->getRightValue()))
->addOrderBy("$baseAlias.rgt asc");
$q->addWhere("$baseAlias.level >= ?", $this->record['level'] - 1);
$q = $this->_tree->returnQueryWithRootId($q, $this->getRootValue());
$result = $q->execute();
if (count($result) <= 0) {
return false;
}
if ($result instanceof Doctrine_Collection) {
$parent = $result->getFirst();
} else if (is_array($result)) {
$parent = array_shift($result);
}
return $parent;
}
PS. Sorry for my english.