Global $user object should be a complete entity

Public

If you're not storing the data in $user->data, you would need to implement hook_user('load') to load in your own custom data into a user object.

Get raw version
php
  1. <?php
  2. //I can't have my fields and values
  3. $user = ($user->uid && isset($user->session)) ? user_load($user->uid) : $user;
  4. //Now I have them
  5. ?>

.. that is the reason I call user_load() in hook_init() not do some db_query().

function hook_init() {
global $user;
//user objects are not completly loaded when restored from session during bootstrap, so there are no extras.....
//$user->session exists only if the object comes from sess_read()

Get raw version
php
  1. <?php
  2. function sess_read($key) {
  3. global $user;
  4.  
  5. // Write and Close handlers are called after destructing objects since PHP 5.0.5
  6. // Thus destructors can use sessions but session handler can't use objects.
  7. // So we are moving session closure before destructing objects.
  8. register_shutdown_function('session_write_close');
  9.  
  10. // Handle the case of first time visitors and clients that don't store cookies (eg. web crawlers).
  11. if (!isset($_COOKIE[session_name()])) {
  12. $user = drupal_anonymous_user();
  13. return '';
  14. }
  15.  
  16. // Otherwise, if the session is still active, we have a record of the client's session in the database.
  17. $user = db_fetch_object(db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key));
  18.  
  19. // We found the client's session record and they are an authenticated user
  20. if ($user && $user->uid > 0) {
  21. // This is done to unserialize the data member of $user
  22. $user = drupal_unpack($user);
  23.  
  24. // Add roles element to $user
  25. $user->roles = array();
  26. $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
  27. $result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $user->uid);
  28. while ($role = db_fetch_object($result)) {
  29. $user->roles[$role->rid] = $role->name;
  30. }
  31. }
  32. // We didn't find the client's record (session has expired), or they are an anonymous user.
  33. else {
  34. $session = isset($user->session) ? $user->session : '';
  35. $user = drupal_anonymous_user($session);
  36. }
  37.  
  38. return $user->session;
  39. }
  40. ?>

and here is the snippet from user_schema, which says, do not use users.data:

'data' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.',
),

Get raw version
php
  1. <?php
  2. $result = db_query('SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d', $user->uid);
  3. while ($role = db_fetch_object($result)) {
  4. $user->roles[$role->rid] = $role->name;
  5. }
  6. //THIS IS THE MISSING LINE!!! or functionality because when sess_read() called, then user.module is not yet included.....
  7. user_module_invoke('load', $array, $user);
  8. ?>