Subscribe: RSS
  • Is there a fast way to get a single modUser record when the only known information is a profile field, such as the user’s phone number?

    This question has been answered by donshakespeare. See the first response.

    • discuss.answer
      I don't know about fast or fastest, but here is a road that will get you to Rome for sure.
      Since the profile items are in a different table and its fields are not found on the id card of the user, you need to make a couple of filtration on the db query level, and then the rest is history!
      
      $userRecord = "";
      $userID = "";
      
      
      $c = $modx->newQuery('modUser'); //query the users table
      $c->innerJoin('modUserProfile','Profile'); //add on the profile table to the query
      $c->where(array( //the filtration is right here. query the joined tables with "WHERE"...
      'active' => true, //is the user active?
      'Profile.phone' => "8009991234", //change the phone number, or use whatever field you want
      // 'Profile.phone:LIKE' => "800%", //phone number starts with 800
      // 'Profile.phone:LIKE' => "%800", //phone number ends in 800
      // 'Profile.phone:LIKE' => "%800%", //phone number contains 800
      ));
      
      $users = $modx->getCollection('modUser',$c); //move from raw query to usable items
      foreach($users as $user){
        $userRecord = $user; //the user record with that phone number and hopefully the only one
        $userID = (string)$user->get("id"); //let's test this record by grabbing the id
      }
      
      /*if the phone# was good $userRecord and $userID will now become available */


      Cheers! [ed. note: donshakespeare last edited this post 6 days, 21 hours ago.]
        TinymceWrapper Suite: Complete back/frontend content solution
        Structures: MODX Content Building Revolutionized

      • Thanks @donshakespeare for the code and the helpful comments!
        • Great! I hope it did what you wanted and is fast enough.
            TinymceWrapper Suite: Complete back/frontend content solution
            Structures: MODX Content Building Revolutionized

          • Here's another way that might be faster (or not):

            $phone = '1298712987';
            
            $profile = $modx->getObject('modUserProfile', array('phone' => $phone));
            
            if ($profile) {
                $user = $modx->getObject('modUser', array('id' => $profile->get('internalKey')));
            } else {
               /* Error */
            }


            I think using $modx->getObjectGraph() would be faster yet, since it would make only one query to the DB, but I'm too tired to work it out. wink
              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
            • Thanks @BobRay! I love that both these code snippets illustrate powerful approaches to interacting with the Modx DB. These are both super helpful!
              • You're welcome. Note that unless you've forced the format of the phone number, users may have added dashes, dots, or parentheses. In which case, your search will fail without a regex search (best done with raw SQL code using MySQL's REGEX operator, which is blindingly fast). You could try it with LIKE, but I'm not sure how reliable or fast it will be.

                I think this would do it:

                $pattern = '/.*129.*871.*2987/';
                  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