<![CDATA[ Getting a user's object by user ID. I'd like to know how it's done. - My Forums]]> https://forums.modx.com/thread/?thread=95652 <![CDATA[Getting a user's object by user ID. I'd like to know how it's done.]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517713
I'm trying to get a user, not the logged in user, by their id, so I can do horrible things with their email address, like spam it. Just kidding, I need it for a Gravatar plugin I'm building and I'm too lazy to DL an existing plugin, install it, and smurf through code.

Already got the Gravatar link generator working and it's hungry for email addresses.

Can I use "getObject('User', $uid );"? Or is the class name Users like the table name? Also, would this be applicable to the user attributes table (i.e. $class = 'UserAttribures';)?]]>
aaronkent Dec 30, 2014, 05:18 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517713
<![CDATA[Re: Getting a user's object by user ID. I'd like to know how it's done.]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517773 ]]> BobRay Dec 31, 2014, 01:27 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517773 <![CDATA[Re: Getting a user's object by user ID. I'd like to know how it's done.]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517768
Thanks, donshakespeare, whitebyte.

BobRay, I couldn't have asked for a more thorough answer. Are you going to be teaching MODX College? I've signed up for it and am in such anticipation, it will be like Christmas when MODX College starts.

In regards to xPDO in general, couldn't I create a constant for the packagepath.modelpath so I don't need to call getOption every time to retrieve it?

I believe this would be much nicer looking...

$prefix = 'pre_';
$packagename ='myPackage';
$classname = 'myClass';

$modx->addPackage( $packagename, MY_MODELPATH, $prefix );

$modx->getObject( $classname, $where );


as opposed to this...

$prefix = 'pre_';
$packagename ='myPackage';
$classname = 'myClass';

$packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/');
$modelpath = $packagepath . 'model/';

$modx->addPackage( $packagename, MY_MODELPATH, $prefix );

$modx->getObject( $classname, $where );


I just eliminated two line of code. But, am I adding too much overhead by adding that modelpath as a constant?

I could call a snippet that adds the constant when I set the custom session variables.]]>
aaronkent Dec 31, 2014, 12:48 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517768
<![CDATA[Re: Getting a user's object by user ID. I'd like to know how it's done. (Best Answer)]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517733
$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());


If you will be doing it with a bunch of users at once, you can speed it up further by only calling prepare() once and using a parameterized query: http://bobsguides.com/blog.html/2014/12/17/using-parameterized-prepared-statements-to-retrieve-data/.]]>
BobRay Dec 30, 2014, 10:44 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517733
<![CDATA[Re: Getting a user's object by user ID. I'd like to know how it's done.]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517727 nuan88 Dec 30, 2014, 10:09 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517727 <![CDATA[Re: Getting a user's object by user ID. I'd like to know how it's done. (Best Answer)]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517718
$user = $modx->getObject('modUser', $id); // basic stuff like username and active/not active
$profile = $user->getOne('Profile'); // longname, phone number, etc
]]>
whitebyte Dec 30, 2014, 07:54 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517718
<![CDATA[Re: Getting a user's object by user ID. I'd like to know how it's done. (Best Answer)]]> https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517717
http://bobsguides.com/revolution-objects.html
Finding Other Objects
In revolution, references to all MODX objects can be retrieved using the $modx->getObject() method.
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); 

Please don't spam them, only the bad ones...]]>
donshakespeare Dec 30, 2014, 07:53 PM https://forums.modx.com/thread/95652/getting-a-user-s-object-by-user-id-i-d-like-to-know-how-it-s-done#dis-post-517717