On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I want to do something like this...

    $user = $modx->getUser();
    foreach ( $user as $key => $value ) {
        $placeholders[$key] .= $value;
    }
    


    But I know that won't work out the way I want it to.

    What is the correct way to iterate through the user table fields?

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

    • I'm not not clear what you're trying to do, but this might be what you want:

      Create a Tpl chunk called UserChunk with the fields you want to show and some formatting:

      <p>
         Username: [[+username]]<br/>
         email: [[+email]]<br/>
         Full Name: [[+fullname]]<br/>
         etc.
      </p>
      


      Put this code in your snippet
      $users = $modx->getCollectionGraph('modUser', '{"Profile":{}}');
      
      $output = "<h3>User List</h3>";
      
      foreach($users as $user) {
          $fields = $user->toArray();
          unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
          if ($user->Profile) {
              $fields = array_merge($user->Profile->toArray(), $fields);
          }
       
          $output .= $modx->getChunk('UserChunk', $fields);
      }
      
      return $output;
      
        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
      • Quote from: BobRay at Aug 13, 2015, 03:41 AM
        I'm not not clear what you're trying to do, but this might be what you want:

        Create a Tpl chunk called UserChunk with the fields you want to show and some formatting:

        <p>
           Username: [[+username]]
        
           email: [[+email]]
        
           Full Name: [[+fullname]]
        
           etc.
        </p>
        


        Put this code in your snippet
        $users = $modx->getCollectionGraph('modUser', '{"Profile":{}}');
        
        $output = "<h3>User List</h3>";
        
        foreach($users as $user) {
            $fields = $user->toArray();
            unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
            if ($user->Profile) {
                $fields = array_merge($user->Profile->toArray(), $fields);
            }
         
            $output .= $modx->getChunk('UserChunk', $fields);
        }
        
        return $output;
        

        Thanks for your reply, BobRay.

        That's a great example of iterating through multiple users, however, I would like to iterate through every field of a single user. I am trying to build a loop that will assign each of a single user's fields into a $placeholders array. So far it seems like I have to use the $user->get('field') to get each of the users fields from the database. I really wish I could iterate through a single users database fields in a loop rather than write out numerous get() calls.
        • Ah, sorry. That's way easier. Just use the Profile snippet (part of the Login package).

          [[!Profile]]
            
          <p>Username: [[+username]]</p>
          <p>Full Name: [[+fullname]]</p>
          <p>Email: [[+email]]</p>
          



          If you prefer to do it in code, it's just like my example above without the loop, and with getObjectGraph() instead of getCollectionGraph().

          $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
          fields = $user->toArray();
          unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
          if ($user->Profile) {
             $fields = array_merge($user->Profile->toArray(), $fields);
          }
            
          $output .= $modx->getChunk('UserChunk', $fields);
          
          
            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
          • Quote from: BobRay at Aug 13, 2015, 06:59 PM
            Ah, sorry. That's way easier. Just use the Profile snippet (part of the Login package).

            [[!Profile]]
              
            <p>Username: [[+username]]</p>
            <p>Full Name: [[+fullname]]</p>
            <p>Email: [[+email]]</p>
            



            If you prefer to do it in code, it's just like my example above without the loop, and with getObjectGraph() instead of getCollectionGraph().

            $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
            $fields = $user->toArray();
            unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
            if ($user->Profile) {
               $fields = array_merge($user->Profile->toArray(), $fields);
            }
              
            $output .= $modx->getChunk('UserChunk', $fields);
            
            

            OK, I see how that works. My placeholders are going to need to be [[!+fi.fieldname]] to work with formit. How do I amend the placeholders that are output? [ed. note: BobRay last edited this post 4 years, 11 months ago.]
            • [[!Profile? &prefix=`fi.`]]
                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
              • Quote from: BobRay at Aug 13, 2015, 06:59 PM
                Ah, sorry. That's way easier. Just use the Profile snippet (part of the Login package).

                $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId);
                
                

                Is there a way to set a prefix on the placeholders using this method?

                I prefer to do it with code because I have my own pre-processor gathering other custom data for the user settings such as language preference and display name/alias. I also have a myriad of settings in another table for privacy settings, user blocking, and security.
                • discuss.answer
                  You might look at ClassExtender for storing the extra user information.

                  To set placeholders with a prefix:

                  $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
                  $fields = $user->toArray();
                  unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
                  if ($user->Profile) {
                     $fields = array_merge($user->Profile->toArray(), $fields);
                  }
                   
                  $modx->toPlaceholders($fields, 'fi');
                    
                  return $modx->getChunk('UserChunk');


                  You can do whatever you want with the $fields array before calling toPlaceholders().
                    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
                  • Quote from: BobRay at Aug 13, 2015, 09:22 PM
                    You might look at ClassExtender for storing the extra user information.

                    To set placeholders with a prefix:

                    $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', $userId); 
                    $fields = $user->toArray();
                    unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
                    if ($user->Profile) {
                       $fields = array_merge($user->Profile->toArray(), $fields);
                    }
                     
                    $modx->toPlaceholders($fields, 'fi');
                      
                    return $modx->getChunk('UserChunk');


                    You can do whatever you want with the $fields array before calling toPlaceholders().

                    Thanks again, BobRay! I've been searching for a couple days trying to figure out the trick to doing this. Now I know. I'll take a look at that article also.