Doctrine 1
  1. Doctrine 1
  2. DC-116

getParent() works as getRoot() in NestedSet behavior

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major 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.

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Vjatseslav Maleev
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: