API16

JTableNested/store: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== Method to store a node in the database table. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<...
 
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
Method to store a node in the database table.
Method to store a node in the database table.


<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JTableNested/store|Edit Descripton]]<nowiki>]</nowiki>
</span>


{{Description:JTableNested/store}}
 
 


===Syntax===
===Syntax===
Line 184: Line 182:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[SeeAlso:JTableNested/store|Edit See Also]]<nowiki>]</nowiki>
 
</span>
{{SeeAlso:JTableNested/store}}


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=store
  category=store
  category=JTableNested
  category=JTableNested
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>

Latest revision as of 02:12, 25 March 2017

Description

Method to store a node in the database table.



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



Examples

Code Examples