We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53432
    • 46 Posts
    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
      • 42562
      • 1,145 Posts
      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 5 years, 9 months ago.]
        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.
        • 53432
        • 46 Posts
        Thanks @donshakespeare for the code and the helpful comments!
          • 42562
          • 1,145 Posts
          Great! I hope it did what you wanted and is fast enough.
            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.
            • 3749
            • 24,544 Posts
            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
              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
              • 53432
              • 46 Posts
              Thanks @BobRay! I love that both these code snippets illustrate powerful approaches to interacting with the Modx DB. These are both super helpful!
                • 3749
                • 24,544 Posts
                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/';
                  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
                  • 54817
                  • 2 Posts
                  I love that both these code snippets illustrate powerful approaches to interacting with the Modx DB. T