API16:JTableUser/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.
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/user.php
Importing
jimport( 'joomla.database.table.user' );
Source Body
function store($updateNulls = false)
{
// Get the table key and key value.
$k = $this->_tbl_key;
$key = $this->$k;
// TODO: This is a dumb way to handle the groups.
// Store groups locally so as to not update directly.
$groups = $this->groups;
unset($this->groups);
// Insert or update the object based on presence of a key value.
if ($key) {
// Already have a table key, update the row.
$return = $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
}
else {
// Don't have a table key, insert the row.
$return = $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);
}
// Handle error if it exists.
if (!$return)
{
$this->setError(strtolower(get_class($this))."::".JText::_('store failed')."<br />".$this->_db->getErrorMsg());
return false;
}
// Reset groups to the local object.
$this->groups = $groups;
unset($groups);
// Store the group data if the user data was saved.
if ($return && is_array($this->groups) && count($this->groups))
{
// Delete the old user group maps.
$this->_db->setQuery(
'DELETE FROM `#__user_usergroup_map`' .
' WHERE `user_id` = '.(int) $this->id
);
$this->_db->query();
// Check for a database error.
if ($this->_db->getErrorNum()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
// Set the new user group maps.
$this->_db->setQuery(
'INSERT INTO `#__user_usergroup_map` (`user_id`, `group_id`)' .
' VALUES ('.$this->id.', '.implode('), ('.$this->id.', ', array_keys($this->groups)).')'
);
$this->_db->query();
// Check for a database error.
if ($this->_db->getErrorNum()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
return true;
}
Examples
Code Examples