We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    I've looked at the doc for $modx->getUser() and it appears that only handles the logged in user and the only parameter is context.

    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';)?

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 42562
      • 1,145 Posts
      Won't this work for modUser?
      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...
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
      • discuss.answer
        • 49529
        • 196 Posts
        $user = $modx->getObject('modUser', $id); // basic stuff like username and active/not active
        $profile = $user->getOne('Profile'); // longname, phone number, etc
        
          • 46886
          • 1,154 Posts
          Do you just want emails, or more data such as extended or custom user data?
          • discuss.answer
            • 3749
            • 24,544 Posts
            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());
            


            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/.
              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
              • 49407
              • 159 Posts
              It was the classname that was messing me up. I was trying to use "User" and kept getting "Could not create object of type: User".

              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.
                • 3749
                • 24,544 Posts
                You'd have to use getOption() to set the MY_MODELPATH constant in order to use the custom System Setting, but I doubt if it involves significant overhead.
                  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