Back to Nested class

Method rebuildPath

public bool
rebuildPath
(mixed $pk = null)
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.
Parameters
  • int $pk Primary key of the node for which to get the path.
Returns
  • bool True on success.
Since
  • 1.7.0
Class: Nested
Project: Joomla

Method rebuildPath - Source code

/**
 * 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.
 *
 * @param   integer  $pk  Primary key of the node for which to get the path.
 *
 * @return  boolean  True on success.
 *
 * @since   1.7.0
 */
public function rebuildPath($pk = null)
{
    $fields = $this->getFields();
    // If there is no alias or path field, just return true.
    if (!\array_key_exists('alias', $fields) || !\array_key_exists('path', $fields)) {
        return true;
    }
    $k = $this->_tbl_key;
    $pk = \is_null($pk) ? $this->{$k} : $pk;
    // Get the aliases for the path from the node to the root node.
    $query = $this->_db->getQuery(true)->select('p.alias')->from($this->_tbl . ' AS n, ' . $this->_tbl . ' AS p')->where('n.lft BETWEEN p.lft AND p.rgt')->where('n.' . $this->_tbl_key . ' = ' . (int) $pk)->order('p.lft');
    $this->_db->setQuery($query);
    $segments = $this->_db->loadColumn();
    // 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.
    $query->clear()->update($this->_tbl)->set('path = ' . $this->_db->quote($path))->where($this->_tbl_key . ' = ' . (int) $pk);
    $this->_db->setQuery($query)->execute();
    // Update the current record's path to the new one:
    $this->path = $path;
    return true;
}