API16

JTable/store

From Joomla! Documentation

< API16:JTable

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]

Method to store a row in the database from the JTable instance properties. If a primary key value is set the row with that primary key value will be updated with the instance property values. If no primary key value is set a new row will be inserted into the database with the properties from the JTable instance.



Syntax[edit]

store($updateNulls=false)
Parameter Name Default Value Description
$updateNulls false True to update fields even if they are null.

Returns[edit]

boolean True on success.

Defined in[edit]

libraries/joomla/database/table.php

Importing[edit]

jimport( 'joomla.database.table' );

Source Body[edit]

public function store($updateNulls = false)
{
        // Initialise variables.
        $k = $this->_tbl_key;

        // If a primary key exists update the object, otherwise insert it.
        if ($this->$k) {
                $stored = $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
        } else {
                $stored = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);
        }

        // If the store failed return false.
        if (!$stored) {
                $this->setError(get_class($this).'::store failed - '.$this->_db->getErrorMsg());
                return false;
        }

        // If the table is not set to track assets return true.
        if (!$this->_trackAssets) {
                return true;
        }

        if ($this->_locked) {
                $this->_unlock();
        }

        //
        // Asset Tracking
        //

        $parentId       = $this->_getAssetParentId();
        $name           = $this->_getAssetName();
        $title          = $this->_getAssetTitle();

        $asset  = JTable::getInstance('Asset');
        $asset->loadByName($name);

        // Check for an error.
        if ($error = $asset->getError()) {
                $this->setError($error);
                return false;
        }

        // Specify how a new or moved node asset is inserted into the tree.
        if (empty($this->asset_id) || $asset->parent_id != $parentId) {
                $asset->setLocation($parentId, 'last-child');
        }

        // Prepare the asset to be stored.
        $asset->parent_id       = $parentId;
        $asset->name            = $name;
        $asset->title           = $title;
        if ($this->_rules instanceof JRules) {
                $asset->rules = (string) $this->_rules;
        }

        if (!$asset->check() || !$asset->store($updateNulls)) {
                $this->setError($asset->getError());
                return false;
        }

        if (empty($this->asset_id)) {
                // Update the asset_id field in this table.
                $this->asset_id = (int) $asset->id;

                $this->_db->setQuery(
                        'UPDATE '.$this->_db->nameQuote($this->_tbl).
                        ' SET asset_id = '.(int) $this->asset_id.
                        ' WHERE '.$this->_db->nameQuote($k).' = '.(int) $this->$k
                );

                if (!$this->_db->query()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                }
        }

        return true;
}



Examples[edit]

Code Examples[edit]