So I have this code in a template:
$thisuser = user_load(array('uid' => '11812'));
which, as far as I understand it, should return a user object for the user with the uid of 11812. And that object should contain an array of roles for that user in this variable:
$thisuser->roles
But that array doesn't exist.
I've traced through the user_load() function in modules/user/user.module with a debugger, and this code should populate the $user object with that roles array:
$result = db_query('SELECT * FROM {users} u WHERE '. implode(' AND ', $query), $params);
if ($user = db_fetch_object($result)) {
$user = drupal_unpack($user);
$user->roles = array();
if ($user->uid) {
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
}
else {
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
}
$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);
while ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name;
}
user_module_invoke('load', $user_info, $user);
}
else {
$user = FALSE;
}
Everything's fine up through this line:
$user = drupal_unpack($user);
but then, when we hit the next line, which should initialize an empty "roles" array in the $user object, nothing happens:
$user->roles = array();
No "roles" array show开发者_如何学运维s up in the object at all. The subsequent db queries work okay, and return the right role information, so this while loop:
while ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name;
}
has the proper values in $role->rid and $role->name each time through the loop, but $user->roles still doesn't exist, and the assignments here have no effect.
I'm beating my head against this - I can't figure out what's going on. I know that I can just query the db directly in order to get a user's roles if I really must, but why wouldn't the user_load() code work?
Anyone know what's going on?
Thanks.
What if you just did user_load(11812)? This has always worked for me up through Drupal 6.
After much agony, I determined this ISN'T a problem in Drupal. It's my stupid debugger, MacGDBp. It has a bug (or an interaction with the macports version of the Xdebug plugin) that causes object instance variables not to be shown in the inspector when there are more than 32 variables.
So dumb.
Anyway, if you're using MacGDBp, beware of this issue.
精彩评论