API16

API16:JTableNested/getRootId

From Joomla! Documentation

Revision as of 02:11, 25 March 2017 by JoomlaWikiBot (talk | contribs) (preparing for archive only)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

Gets the ID of the root item in the tree



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;
}



Examples

Code Examples