API16:JTableNested/getRootId
From Joomla! Documentation
Description
Gets the ID of the root item in the tree
Template:Description:JTableNested/getRootId
Syntax
getRootId()
Returns
mixed The ID of the root row, or false and the internal error is set.
Defined in
libraries/joomla/database/tablenested.php
Importing
jimport( 'joomla.database.tablenested' );
Source Body
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;
}
[Edit See Also] Template:SeeAlso:JTableNested/getRootId
Examples
<CodeExamplesForm />