JTableNested/getRootId
From Joomla! Documentation
< API16:JTableNested
The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Gets the ID of the root item in the tree
Syntax[edit]
getRootId()
Returns[edit]
mixed The ID of the root row, or false and the internal error is set.
Defined in[edit]
libraries/joomla/database/tablenested.php
Importing[edit]
jimport( 'joomla.database.tablenested' );
Source Body[edit]
public function getRootId()
{
// Get the root item.
$k = $this->_tbl_key;
try {
// Test for a unique record with parent_id = 0
$this->_db->setQuery(
'SELECT '.$this->_db->nameQuote($k).
' FROM '.$this->_tbl .
' WHERE `parent_id` = 0'
);
$result = $this->_db->loadResultArray();
if ($this->_db->getErrorNum()) {
throw new Exception($this->_db->getErrorMsg());
}
if (count($result) == 1) {
$parentId = $result[0];
} else {
// Test for a unique record with lft = 0
$this->_db->setQuery(
'SELECT '.$this->_db->nameQuote($k).
' FROM '.$this->_tbl .
' WHERE `lft` = 0'
);
$result = $this->_db->loadResultArray();
if ($this->_db->getErrorNum()) {
throw new Exception($this->_db->getErrorMsg());
}
if (count($result) == 1) {
$parentId = $result[0];
} else if (property_exists($this, 'alias')) {
// Test for a unique record with lft = 0
$this->_db->setQuery(
'SELECT '.$this->_db->nameQuote($k).
' FROM '.$this->_tbl .
' WHERE `alias` = '.$this->_db->quote('root')
);
$result = $this->_db->loadResultArray();
if ($this->_db->getErrorNum()) {
throw new Exception($this->_db->getErrorMsg());
}
if (count($result) == 1) {
$parentId = $result[0];
} else {
throw new Exception(JText::_('JTable_Error_Root_node_not_found'));
}
} else {
throw new Exception(JText::_('JTable_Error_Root_node_not_found'));
}
}
} catch (Exception $e) {
$this->setError($e->getMessage());
return false;
}
return $parentId;
}
Examples[edit]
Code Examples[edit]