Back to Nested class

Method rebuild

public int
rebuild
(mixed $parentId = null, mixed $leftId = 0, mixed $level = 0, mixed $path = '')
Method to recursively rebuild the whole nested set tree.
Parameters
  • int $parentId The root of the tree to rebuild.
  • int $leftId The left id to start with in building the tree.
  • int $level The level to assign to the current nodes.
  • string $path The path to the current nodes.
Returns
  • int 1 + value of root rgt on success, false on failure
Since
  • 1.7.0
-
  • \RuntimeException on database error.
Class: Nested
Project: Joomla

Method rebuild - Source code

/**
 * Method to recursively rebuild the whole nested set tree.
 *
 * @param   integer  $parentId  The root of the tree to rebuild.
 * @param   integer  $leftId    The left id to start with in building the tree.
 * @param   integer  $level     The level to assign to the current nodes.
 * @param   string   $path      The path to the current nodes.
 *
 * @return  integer  1 + value of root rgt on success, false on failure
 *
 * @since   1.7.0
 * @throws  \RuntimeException on database error.
 */
public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = '')
{
    // If no parent is provided, try to find it.
    if ($parentId === null) {
        // Get the root item.
        $parentId = $this->getRootId();
        if ($parentId === false) {
            return false;
        }
    }
    $query = $this->_db->getQuery(true);
    // Build the structure of the recursive query.
    if (!isset($this->_cache['rebuild.sql'])) {
        $query->clear()->select($this->_tbl_key . ', alias')->from($this->_tbl)->where('parent_id = %d');
        // If the table has an ordering field, use that for ordering.
        if ($this->hasField('ordering')) {
            $query->order('parent_id, ' . $this->_db->quoteName($this->getColumnAlias('ordering')) . ', lft');
        } else {
            $query->order('parent_id, lft');
        }
        $this->_cache['rebuild.sql'] = (string) $query;
    }
    // Make a shortcut to database object.
    // Assemble the query to find all children of this node.
    $this->_db->setQuery(sprintf($this->_cache['rebuild.sql'], (int) $parentId));
    $children = $this->_db->loadObjectList();
    // The right value of this node is the left value + 1
    $rightId = $leftId + 1;
    // Execute this function recursively over all children
    foreach ($children as $node) {
        /*
         * $rightId is the current right value, which is incremented on recursion return.
         * Increment the level for the children.
         * Add this item's alias to the path (but avoid a leading /)
         */
        $rightId = $this->rebuild($node->{$this->_tbl_key}, $rightId, $level + 1, $path . (empty($path) ? '' : '/') . $node->alias);
        // If there is an update failure, return false to break out of the recursion.
        if ($rightId === false) {
            return false;
        }
    }
    // We've got the left value, and now that we've processed
    // the children of this node we also know the right value.
    $query->clear()->update($this->_tbl)->set('lft = ' . (int) $leftId)->set('rgt = ' . (int) $rightId)->set('level = ' . (int) $level)->set('path = ' . $this->_db->quote($path))->where($this->_tbl_key . ' = ' . (int) $parentId);
    $this->_db->setQuery($query)->execute();
    // Return the right value of this node + 1.
    return $rightId + 1;
}