Actions

API16

JTableNested/store

From Joomla! Documentation

< API16:JTableNested
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Method to store a node in the database table.

[Edit Descripton]

Description:JTableNested/store

Syntax

store($updateNulls=false)
Parameter Name Default Value Description
$updateNulls false True to update null values as well.

Returns

boolean True on success.

Defined in

libraries/joomla/database/tablenested.php

Importing

jimport( 'joomla.database.tablenested' );

Source Body

public function store($updateNulls = false)
{
        // Initialise variables.
        $k = $this->_tbl_key;
 
        if ($this->_debug) {
                echo "\n".get_class($this)."::store\n";
                $this->_logtable(true, false);
        }
        /*
         * If the primary key is empty, then we assume we are inserting a new node into the
         * tree.  From this point we would need to determine where in the tree to insert it.
         */
        if (empty($this->$k)) {
                /*
                 * We are inserting a node somewhere in the tree with a known reference
                 * node.  We have to make room for the new node and set the left and right
                 * values before we insert the row.
                 */
                if ($this->_location_id >= 0) {
                        // Lock the table for writing.
                        if (!$this->_lock()) {
                                // Error message set in lock method.
                                return false;
                        }
 
                        // We are inserting a node relative to the last root node.
                        if ($this->_location_id == 0) {
                                // Get the last root node as the reference node.
                                $this->_db->setQuery(
                                        'SELECT `'.$this->_tbl_key.'`, `parent_id`, `level`, `lft`, `rgt`' .
                                        ' FROM `'.$this->_tbl.'`' .
                                        ' WHERE `parent_id` = 0' .
                                        ' ORDER BY `lft` DESC',
                                        0, 1
                                );
                                $reference = $this->_db->loadObject();
 
                                // Check for a database error.
                                if ($this->_db->getErrorNum()) {
                                        $this->setError($this->_db->getErrorMsg());
                                        $this->_unlock();
                                        return false;
                                }
                                if ($this->_debug) {
                                        $this->_logtable(false);
                                }
                        }
 
                        // We have a real node set as a location reference.
                        else {
                                // Get the reference node by primary key.
                                if (!$reference = $this->_getNode($this->_location_id)) {
                                        // Error message set in getNode method.
                                        $this->_unlock();
                                        return false;
                                }
                        }
 
                        // Get the reposition data for shifting the tree and re-inserting the node.
                        if (!($repositionData = $this->_getTreeRepositionData($reference, 2, $this->_location))) {
                                // Error message set in getNode method.
                                $this->_unlock();
                                return false;
                        }
 
                        // Create space in the tree at the new location for the new node in left ids.
                        $this->_db->setQuery(
                                'UPDATE `'.$this->_tbl.'`' .
                                ' SET `lft` = `lft` + 2' .
                                ' WHERE '.$repositionData->left_where
                        );
                        $this->_db->query();
 
                        // Check for a database error.
                        if ($this->_db->getErrorNum()) {
                                $this->setError($this->_db->getErrorMsg());
                                $this->_unlock();
                                return false;
                        }
                        if ($this->_debug) {
                                $this->_logtable();
                        }
 
                        // Create space in the tree at the new location for the new node in right ids.
                        $this->_db->setQuery(
                                'UPDATE `'.$this->_tbl.'`' .
                                ' SET `rgt` = `rgt` + 2' .
                                ' WHERE '.$repositionData->right_where
                        );
                        $this->_db->query();
 
                        // Check for a database error.
                        if ($this->_db->getErrorNum()) {
                                $this->setError($this->_db->getErrorMsg());
                                $this->_unlock();
                                return false;
                        }
                        if ($this->_debug) {
                                $this->_logtable();
                        }
 
                        // Set the object values.
                        $this->parent_id        = $repositionData->new_parent_id;
                        $this->level            = $repositionData->new_level;
                        $this->lft                      = $repositionData->new_lft;
                        $this->rgt                      = $repositionData->new_rgt;
                } else {
                        // Negative parent ids are invalid
                        $this->setError(JText::_('Invalid_Parent'));
                        return false;
                }
        }
 
        /*
         * If we have a given primary key then we assume we are simply updating this
         * node in the tree.  We should assess whether or not we are moving the node
         * or just updating its data fields.
         */
        else {
                // If the location has been set, move the node to its new location.
                if ($this->_location_id > 0)
                {
                        if (!$this->move($this->_location_id, $this->_location, $this->$k)) {
                                // Error message set in move method.
                                return false;
                        }
                }
 
                // Lock the table for writing.
                if (!$this->_lock()) {
                        // Error message set in lock method.
                        return false;
                }
        }
 
        // Store the row to the database.
        if (!parent::store()) {
                $this->_unlock();
                return false;
        }
        if ($this->_debug) {
                $this->_logtable();
        }
 
        // Unlock the table for writing.
        $this->_unlock();
 
        return true;
}

[Edit See Also] SeeAlso:JTableNested/store

Examples

<CodeExamplesForm />