We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13505
    • 8 Posts
    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. I’ve done some searching but haven’t been able to get anything to work. Any insight?

    Thanks,
    Jim
      • 3749
      • 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');
      

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 13505
        • 8 Posts
        Thanks Bob, that got me close it ended up being the following that worked for me:
        $profile = $modx->getObject('modUserProfile', $id);
        $fullname = $profile->get('fullname');
        return $fullname;
        


        This is solved now.
          • 15083
          • 697 Posts
          Quote from: jameswwright at Jan 26, 2011, 03:09 AM

          Thanks Bob, that got me close it ended up being the following that worked for me:
          $profile = $modx->getObject('modUserProfile', $id);
          $fullname = $profile->get('fullname');
          return $fullname;
          


          erm....thats the same as what bob gave you smiley
            • 3749
            • 24,544 Posts
            Not quite. wink

            His method might be a little faster because there’s no array to dereference.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
            • 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.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 3749
                • 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');
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting