API16

JTableUser/load: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== 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. <span class="editsection" style="font-si...
 
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
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.
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.


<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JTableUser/load|Edit Descripton]]<nowiki>]</nowiki>
</span>


{{Description:JTableUser/load}}
 
 


===Syntax===
===Syntax===
Line 102: Line 100:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[SeeAlso:JTableUser/load|Edit See Also]]<nowiki>]</nowiki>
 
</span>
{{SeeAlso:JTableUser/load}}


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=load
  category=load
  category=JTableUser
  category=JTableUser
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>

Latest revision as of 02:12, 25 March 2017

Description

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.



Syntax

load($userId=null)
Parameter Name Default Value Description
$userId null $userId An optional user id.

Returns

boolean True on success, false on failure.

Defined in

libraries/joomla/database/table/user.php

Importing

jimport( 'joomla.database.table.user' );

Source Body

function load($userId = null)
{
        // 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();

        // Load the user data.
        $this->_db->setQuery(
                'SELECT *' .
                ' FROM #__users' .
                ' WHERE id = '.(int) $userId
        );
        $data = (array) $this->_db->loadAssoc();

        // Check for an error message.
        if ($this->_db->getErrorNum()) {
                $this->setError($this->_db->getErrorMsg());
                return false;
        }

        if(!count($data))
        {
                return false;
        }
        // Bind the data to the table.
        $return = $this->bind($data);

        if ($return !== false)
        {
                // Load the user groups.
                $this->_db->setQuery(
                        'SELECT g.id, g.title' .
                        ' FROM #__usergroups AS g' .
                        ' JOIN #__user_usergroup_map AS m ON m.group_id = g.id' .
                        ' WHERE m.user_id = '.(int) $userId
                );
                $result = $this->_db->loadObjectList();
                $groups = array();

                // Check for an error message.
                if ($this->_db->getErrorNum()) {
                        $this->setError($this->_db->getErrorMsg());
                        return false;
                }

                // Create an array of groups.
                for ($i = 0, $n = count($result); $i < $n; $i++)
                {
                        $groups[$result[$i]->id] = $result[$i]->title;
                }

                // Add the groups to the user data.
                $this->groups = $groups;
        }

        return $return;
}



Examples

Code Examples