We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    How would I go about getting a modUser object by 'fieldname', such as
    $user = $modx->getObject('modUser', array('email' => '[email protected]'));

    All this does is throw an error (not entirely unexpected, but one did have hopes)
    [2] => Unknown column 'modUser.email' in 'where clause'
      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
      You need sleep. I know you know the email field is in the User Profile, not in modUser. wink


      $email = $modx->getOption('email', $scriptProperties);
      $c = $modx->newQuery('modUserProfile');
      $c->where(array('email' => $email));
      $c->limit(1);
      $profile = $modx->getCollection('modUserProfile', $c );
      
      if ($profile) {
          $user = $profile->getOne('User');
      }
      


      Actually, since you only want one, I think you could use getObject() with the same arguments instead of getCollection(). I'm not sure if it would be faster.
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I just woke up. I tried it with modProfile, which of course didn't work. It's been a busy last few days wink

        getCollection is probably better for flexibility in this case; this may want to be done for a single user, or for all users who have the value being searched for.

        In this particular case, I was only looking for the user ID, so I'd need the internalKey from the modUserProfile.

        Now to go test that with
        'extended:LIKE' => %$value%
          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          That seems like an awfully convoluted way to just get user IDs based on a value in the user_attributes field.
            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
            Another good reason for extending modUser. wink

            AFAIK, that field is not indexed and since it contains a JSON string, you have to use LIKE, which can be slow and unreliable unless you're careful with the values.

            With the extended class, it would just be:

            $users = $modx->getCollection('extUser', array('myField' => 'someValue'));
            foreach ($users as $user) {
              $id = $user->get('userdata_id');
            }


            Extended fields are not really meant for searching or sorting. I'll be interested to see what happens with user fields in 3.0.
              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
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Better use of the user_settings field, maybe. It's already there, but hardly gets used.
                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
                I can't agree. The user_settings field is another JSON field like the 'extended' field, with the same drawbacks. Cramming multiple data items you might want to search or sort by into a single field is never a good idea, imo.

                I expect that we'll see a more generic solution that lets us create and delete extra user fields that exist in their own table. That would allow cool stuff like a form to add a user field, with a checkbox for showing or not showing the field on the Create/Edit User panel (how sweet would that be? wink ).




                  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
                  • 42318
                  • 25 Posts
                  Hey, I would like to search for users by a fieldname, and return an array of users.

                  My query would need to be with a LIKE clause

                  I can't find the right syntax for this.

                  Can anybody help please?

                  Thanks!
                    • 3749
                    • 24,544 Posts
                    Which user field? The method differs depending on which field you want to check.
                      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
                      • 42318
                      • 25 Posts
                      I want to search on the "comment" field.

                      I'm using this to store some ids, seperated by "|"

                      I then want to do a LIKE query on this field.

                      Thanks

                      (I'll take a look at your class extender when I get a chance, but just want a quick fix for now ;-)