Back to User class

Method store

public bool
store
(mixed $updateNulls = true)
Method to store a row in the database from the Table instance properties.
Parameters
  • bool $updateNulls True to update fields even if they are null.
Returns
  • bool True on success.
Since
  • 1.7.0
Class: User
Project: Joomla

Method store - Source code

/**
 * Method to store a row in the database from the Table 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 Table instance.
 *
 * @param   boolean  $updateNulls  True to update fields even if they are null.
 *
 * @return  boolean  True on success.
 *
 * @since   1.7.0
 */
public function store($updateNulls = true)
{
    // 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.
        $this->_db->updateObject($this->_tbl, $this, $this->_tbl_key, $updateNulls);
    } else {
        // Don't have a table key, insert the row.
        $this->_db->insertObject($this->_tbl, $this, $this->_tbl_key);
    }
    // Reset groups to the local object.
    $this->groups = $groups;
    $query = $this->_db->getQuery(true);
    // Store the group data if the user data was saved.
    if (\is_array($this->groups) && \count($this->groups)) {
        $uid = (int) $this->id;
        // Grab all usergroup entries for the user
        $query->clear()->select($this->_db->quoteName('group_id'))->from($this->_db->quoteName('#__user_usergroup_map'))->where($this->_db->quoteName('user_id') . ' = :userid')->bind(':userid', $uid, ParameterType::INTEGER);
        $this->_db->setQuery($query);
        $result = $this->_db->loadObjectList();
        // Loop through them and check if database contains something $this->groups does not
        if (\count($result)) {
            $mapGroupId = [];
            foreach ($result as $map) {
                if (\array_key_exists($map->group_id, $this->groups)) {
                    // It already exists, no action required
                    unset($groups[$map->group_id]);
                } else {
                    $mapGroupId[] = (int) $map->group_id;
                }
            }
            if (\count($mapGroupId)) {
                $query->clear()->delete($this->_db->quoteName('#__user_usergroup_map'))->where($this->_db->quoteName('user_id') . ' = :uid')->whereIn($this->_db->quoteName('group_id'), $mapGroupId)->bind(':uid', $uid, ParameterType::INTEGER);
                $this->_db->setQuery($query);
                $this->_db->execute();
            }
        }
        // If there is anything left in this->groups it needs to be inserted
        if (\count($groups)) {
            // Set the new user group maps.
            $query->clear()->insert($this->_db->quoteName('#__user_usergroup_map'))->columns([$this->_db->quoteName('user_id'), $this->_db->quoteName('group_id')]);
            foreach ($groups as $group) {
                $query->values(implode(',', $query->bindArray([$this->id, $group], [ParameterType::INTEGER, ParameterType::INTEGER])));
            }
            $this->_db->setQuery($query);
            $this->_db->execute();
        }
        unset($groups);
    }
    // If a user is blocked, delete the cookie login rows
    if ($this->block == 1) {
        $query->clear()->delete($this->_db->quoteName('#__user_keys'))->where($this->_db->quoteName('user_id') . ' = :user_id')->bind(':user_id', $this->username);
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    return true;
}