API16:JTableNested/rebuild
From Joomla! Documentation
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Contents |
Description
Method to recursively rebuild the whole nested set tree.
Description:JTableNested/rebuild
Syntax
rebuild($parentId=null, $leftId=0, $level=0, $path= '')
| Parameter Name | Default Value | Description |
|---|---|---|
| $parentId | null | The root of the tree to rebuild. |
| $leftId | 0 | The left id to start with in building the tree. |
| $level | 0 | The level to assign to the current nodes. |
| $path | The path to the current nodes. |
Returns
boolean True on success
Defined in
libraries/joomla/database/tablenested.php
Importing
jimport( 'joomla.database.tablenested' );
Source Body
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; } } // Build the structure of the recursive query. if (!isset($this->_cache['rebuild.sql'])) { $query = $this->_db->getQuery(true); $query->select('id, alias'); $query->from($this->_tbl); $query->where('parent_id = %d'); // If the table has an `ordering` field, use that for ordering. if (property_exists($this, 'ordering')) { $query->order('parent_id, ordering, lft'); } else { $query->order('parent_id, lft'); } $this->_cache['rebuild.sql'] = (string) $query; } // Make a shortcut to database object. $db = &$this->_db; // Assemble the query to find all children of this node. $db->setQuery(sprintf($this->_cache['rebuild.sql'], (int) $parentId)); $children = $db->loadObjectList(); // The right value of this node is the left value + 1 $rightId = $leftId + 1; // execute this function recursively over all children for ($i = 0, $n = count($children); $i < $n; $i++) { // $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($children[$i]->id, $rightId, $level + 1, $path.(empty($path) ? '' : '/').$children[$i]->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. $db->setQuery( 'UPDATE '. $this->_tbl . ' SET lft = '. (int) $leftId .', rgt = '. (int) $rightId . ' , level = '.(int) $level . ' , path = '.$db->quote($path) . ' WHERE id = '. (int)$parentId ); // If there is an update failure, return false to break out of the recursion. if (!$db->query()) { $this->setError($db->getErrorMsg()); return false; } // Return the right value of this node + 1. return $rightId + 1; }
[Edit See Also] SeeAlso:JTableNested/rebuild
Examples
<CodeExamplesForm />
