Users

Getting Values from a User's Profile

hearty fare

User Profile Values

Quite often you'll want to get a value from a given user's profile. As long as you know the user's ID, there's a quick and easy way to do that using the API getObject() function.

jameswwright asked...

Hello, I’m using ModX Revolution and would essentially like to do this:

    SELECT fullname FROM modx_user_attributes WHERE id = 6
  

but in a snippet using the MODX api...

The Solution:

BobRay says...

If all you have is the user ID, I think this will do it:

    $id = '6';
    $profile = $modx->getObject('modUserProfile', array('internalKey' => $id));
    $fullname = $profile->get('fullname');
  

bezumkin says (via Skype)

I always do a check of objects:

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

Otherwise you will get E_FATAL_ERROR if the user profile was not found.

What's the Story?

The modUserProfile class is based on the user_attributes table in the database. The 'internalKey' field corrsponds to the 'id' field of the users table. This is a quick way to get a known user's Profile object, then get whatever value you want from that user's Profile object.

And as bezumkin says, you should always check to make sure that the object requested was successfully instantiated.