JTableNested/rebuildPath: Difference between revisions
From Joomla! Documentation
m clean up |
m preparing for archive only |
||
| Line 4: | Line 4: | ||
===Syntax=== | ===Syntax=== | ||
| Line 78: | Line 78: | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=rebuildPath | category=rebuildPath | ||
category=JTableNested | category=JTableNested | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 02:12, 25 March 2017
Description
Method to rebuild the node's path field from the alias values of the nodes from the current node to the root node of the tree.
Syntax
rebuildPath($pk=null)
| Parameter Name | Default Value | Description |
|---|---|---|
| $pk | null | Primary key of the node for which to get the path. |
Returns
boolean True on success.
Defined in
libraries/joomla/database/tablenested.php
Importing
jimport( 'joomla.database.tablenested' );
Source Body
public function rebuildPath($pk = null)
{
// If there is no alias or path field, just return true.
if (!property_exists($this, 'alias') || !property_exists($this, 'path')) {
return true;
}
// Initialise variables.
$k = $this->_tbl_key;
$pk = (is_null($pk)) ? $this->$k : $pk;
// Get the aliases for the path from the node to the root node.
$this->_db->setQuery(
'SELECT p.alias' .
' FROM '.$this->_tbl.' AS n, '.$this->_tbl.' AS p' .
' WHERE n.lft BETWEEN p.lft AND p.rgt' .
' AND n.'.$this->_tbl_key.' = '. (int) $pk .
' ORDER BY p.lft'
);
$segments = $this->_db->loadResultArray();
// Make sure to remove the root path if it exists in the list.
if ($segments[0] == 'root') {
array_shift($segments);
}
// Build the path.
$path = trim(implode('/', $segments), ' /\\');
// Update the path field for the node.
$this->_db->setQuery(
'UPDATE `'.$this->_tbl.'`' .
' SET `path` = '.$this->_db->quote($path) .
' WHERE `'.$this->_tbl_key.'` = '.(int) $pk
);
$this->_db->query();
// Check for a database error.
if ($this->_db->getErrorNum()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
return true;
}
Examples
Code Examples