Back to User class

Method load

public bool
load
(mixed $userId = null, mixed $reset = true)
Method to load a user, user groups, and any other necessary data from the database so that it can be bound to the user object.
Parameters
  • int $userId An optional user id.
  • bool $reset False if row not found or on error (internal error state set in that case).
Returns
  • bool True on success, false on failure.
Since
  • 1.7.0
Class: User
Project: Joomla

Method load - Source code

/**
 * Method to load a user, user groups, and any other necessary data
 * from the database so that it can be bound to the user object.
 *
 * @param   integer  $userId  An optional user id.
 * @param   boolean  $reset   False if row not found or on error
 *                           (internal error state set in that case).
 *
 * @return  boolean  True on success, false on failure.
 *
 * @since   1.7.0
 */
public function load($userId = null, $reset = true)
{
    // Get the id to load.
    if ($userId !== null) {
        $this->id = $userId;
    } else {
        $userId = $this->id;
    }
    // Check for a valid id to load.
    if ($userId === null) {
        return false;
    }
    // Reset the table.
    $this->reset();
    $userId = (int) $userId;
    // Load the user data.
    $query = $this->_db->getQuery(true)->select('*')->from($this->_db->quoteName('#__users'))->where($this->_db->quoteName('id') . ' = :userid')->bind(':userid', $userId, ParameterType::INTEGER);
    $this->_db->setQuery($query);
    $data = (array) $this->_db->loadAssoc();
    if (!\count($data)) {
        return false;
    }
    // Convert email from punycode
    $data['email'] = PunycodeHelper::emailToUTF8($data['email']);
    // Bind the data to the table.
    $return = $this->bind($data);
    if ($return !== false) {
        // Load the user groups.
        $query->clear()->select($this->_db->quoteName('g.id'))->select($this->_db->quoteName('g.title'))->from($this->_db->quoteName('#__usergroups', 'g'))->join('INNER', $this->_db->quoteName('#__user_usergroup_map', 'm'), $this->_db->quoteName('m.group_id') . ' = ' . $this->_db->quoteName('g.id'))->where($this->_db->quoteName('m.user_id') . ' = :muserid')->bind(':muserid', $userId, ParameterType::INTEGER);
        $this->_db->setQuery($query);
        // Add the groups to the user data.
        $this->groups = $this->_db->loadAssocList('id', 'id');
    }
    return $return;
}