-
- 24,544 Posts
How you can best do it depends on what you have a the moment.
If you already have the user object (as $user ), you can use this:
<?php
$profile = $user->getOne('Profile'); // Note the capital 'P'
$fullname = $profile->get('fullname');
The current user is always available as $modx->user.
If all you have is the user ID, I think this will do it:
<?php
$id = '6';
$profile = $modx->getObject('modUserProfile', array('internalKey' => $id));
$fullname = $profile->get('fullname');
-
- 24,544 Posts
Not quite.
His method might be a little faster because there’s no array to dereference.
The only problem with the last method is that there is no guarantee that the Profile's ID will be the same as the User's ID.
The $id as used in the getObject function in this manner is actually referring to the id of the row in the user_attributes table. So, while in general it will almost always match, it is an auto-increment field for that table and there are any number of things that can unsync those fields.
Using the internalKey is much safer.
-
- 24,544 Posts
Susan is correct, the id field of the profile is arbitrary and will only work if you create the user profile at the same time as the user. Even them after you've deleted and added a few users, the odds are that it won't be the user's actual ID for all users. The internalKey field of the profile is guaranteed to match the user's MODX ID. That's why I suggested this if you know the user's ID:
<?php
$id = '6'; // or whatever
$profile = $modx->getObject('modUserProfile', array('internalKey' => $id));
$fullname = $profile->get('fullname');