Back to Nested class

Method getRootId

public mixed
getRootId
()
Gets the ID of the root item in the tree
Returns
  • mixed The primary id of the root row, or false if not found and the internal error is set.
Since
  • 1.7.0
Class: Nested
Project: Joomla

Method getRootId - Source code

/**
 * Gets the ID of the root item in the tree
 *
 * @return  mixed  The primary id of the root row, or false if not found and the internal error is set.
 *
 * @since   1.7.0
 */
public function getRootId()
{
    if ((int) self::$root_id > 0) {
        return self::$root_id;
    }
    // Get the root item.
    $k = $this->_tbl_key;
    // Test for a unique record with parent_id = 0
    $query = $this->_db->getQuery(true)->select($k)->from($this->_tbl)->where('parent_id = 0');
    $result = $this->_db->setQuery($query)->loadColumn();
    if (\count($result) == 1) {
        self::$root_id = $result[0];
        return self::$root_id;
    }
    // Test for a unique record with lft = 0
    $query->clear()->select($k)->from($this->_tbl)->where('lft = 0');
    $result = $this->_db->setQuery($query)->loadColumn();
    if (\count($result) == 1) {
        self::$root_id = $result[0];
        return self::$root_id;
    }
    $fields = $this->getFields();
    if (\array_key_exists('alias', $fields)) {
        // Test for a unique record alias = root
        $query->clear()->select($k)->from($this->_tbl)->where('alias = ' . $this->_db->quote('root'));
        $result = $this->_db->setQuery($query)->loadColumn();
        if (\count($result) == 1) {
            self::$root_id = $result[0];
            return self::$root_id;
        }
    }
    $e = new \UnexpectedValueException(sprintf('%s::getRootId', \get_class($this)));
    $this->setError($e);
    self::$root_id = false;
    return false;
}