Users

Getting a Specified User Object

Getting User Object

You want to get a given user's Profile so you can access his data.

arronkent asked...

I'm trying to get a user, not the logged in user, by their id, so I can do things with their email address.

The Solution:

donshakespeare says...

You can "get" a MODX object like this:

$object = $modx->getObject('object-class-name',array(
       'name' => 'object-name' ));
Or, to get the object by ID number:
$object = $modx->getObject('object-class-name',$object-id);

whitebyte says...

$user = $modx->getObject('modUser', $id); // basic stuff like username and active/not active
$profile = $user->getOne('Profile'); // longname, phone number, etc

BobRay says...

FYI, if this code will run on a live site, it's a good practice to test the return values of the getObject() and getOne() calls before calling get() on those objects:

$user = $modx->getObject('modUser', $uid);
if ($user) {
   $username = $user->get('username');
   $profile = $user->getOne('Profile');
   if ($profile) {
       $email = $profile->get('email');
   }
}

If everything you want is in the user profile, you can make things faster by getting it directly rather than going through the user object:

$profile = $modx->getObject('modUserProfile', array('internalKey' => $uid));
if ($profile) {
   $email = $profile->get('email');
}

If all you want is the email, and if this will be happening a lot, you can make it *really* fast by doing this:

/* Get user Email */
$query = $modx->newQuery('modUserProfile', array(
    'internalKey' => $uid,
));
$query->select('email');
$email = $modx->getValue($query->prepare());

What's the Story?

MODX keeps user data in two tables in the database. The user ID, username and password are in the 'users' table, while the rest of the user data is in the 'user_attributes' table. The key to connect a user with his attributes is the internalKey field in the user_attributes table, which holds the user ID.

The getObject() API function makes it simple to get the User object, which will have the id, username and password. You can use a User object's built-in get('Profile') function to get the user's Profile object, and then use its get() function to get all of the values you need.

If you aren't going to need the User object's data for anything, you can skip that step and just get a UserProfile object, using that internalKey if you know the user ID. You can further refine that by using a newQuery() object to just select one item from a modUserProfile object. Let's take a closer look at that query.

/* Get user Email */
$query = $modx->newQuery('modUserProfile', array(
    'internalKey' => $uid,
));
$query->select('email');
$email = $modx->getValue($query->prepare());

First, we instantiate a new Query object, specifying what kind of MODX object we're looking for, and an array of WHERE conditions, in this case the user_attributes table's 'internalKey' value is equal to the user's ID.

$query = $modx->newQuery('modUserProfile', array(
    'internalKey' => $uid,
));

Then we specify that we want to select one field from the table, the 'email' field.

$query->select('email');

Finally, we tell MODX to prepare the query - generate the actual SQL - and use getValue() to run that query and return the single value from the query results - the user's email address.

$email = $modx->getValue($query->prepare());

Two very useful links were provided in this conversation:


Susan Ottwell
February 2015