<![CDATA[ Is there a way to iterate through the user object and set the values of the user table into a $placeholders array? - My Forums]]> https://forums.modx.com/thread/?thread=97995 <![CDATA[Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-529933
$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?]]>
aaronkent Aug 12, 2015, 07:22 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-529933
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530072 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.]]>
aaronkent Aug 14, 2015, 06:18 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530072
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array? (Best Answer)]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530014 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().]]>
BobRay Aug 13, 2015, 04:22 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530014
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530008 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.]]>
aaronkent Aug 13, 2015, 02:58 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530008
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530006
[[!Profile? &prefix=`fi.`]]
]]>
BobRay Aug 13, 2015, 02:35 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530006
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530004 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?]]>
aaronkent Aug 13, 2015, 02:28 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530004
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530001
[[!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);

]]>
BobRay Aug 13, 2015, 01:59 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-530001
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-529998 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.]]>
aaronkent Aug 13, 2015, 01:45 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-529998
<![CDATA[Re: Is there a way to iterate through the user object and set the values of the user table into a $placeholders array?]]> https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-529937
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;
]]>
BobRay Aug 12, 2015, 10:41 PM https://forums.modx.com/thread/97995/is-there-a-way-to-iterate-through-the-user-object-and-set-the-values-of-the-user-table-into-a-placeholders-array#dis-post-529937