API16:JTable/store
From Joomla! Documentation
Description
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.
{{subst:Description:JTable/store}}
Syntax
store($updateNulls=false)
| Parameter Name | Default Value | Description |
|---|---|---|
| $updateNulls | false | True to update fields even if they are null. |
Returns
boolean True on success.
Defined in
libraries/joomla/database/table.php
Importing
jimport( 'joomla.database.table' );
Source Body
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;
}
{{subst:SeeAlso:JTable/store}}
Examples
<CodeExamplesForm />