I have extended my users following Bob Ray's ClassExtender. I would now like to include the extended fields in a query listing our users by state. This code works to a point, but what is the correct syntax to include the extended Data fields. Any suggestions,hints, or not so gentle prompting would be most welcome at this point.
$csaacP = "AB";
$csaacmember = $modx->getCollection('modUserProfile',array('state'=>$csaacP));
foreach($csaacmember as $Pmembers) {
$fields = $Pmembers->toArray();
$output .= $modx->getChunk('UsersProvTpl', $fields);
}
return $output;
Thanks
Jim
Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.
-
☆ A M B ☆
- 24,524 Posts
Have you looked at the GetExtUsers snippet that is part of the ClassExtender package?
Quote from: sottwell at Dec 19, 2014, 10:29 AMHave you looked at the GetExtUsers snippet that is part of the ClassExtender package?
Thanks Susan. I tried that even though it is designed to search on the extended (Data) fields rather than the user profile. Searching on and extended field "LastName" for example returns all entries in the database. Can't for the life of me see how to search the profile by state and pick up the extended fields for each user. Problby have been trying too hard and too long.
Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.
-
☆ A M B ☆
- 24,524 Posts
You should be able to use the "where" property. This looks like it works pretty much the same way that getResources' "where" property works, it takes a JSON string of key: value pairs, so something like
&where=`{"state:=":"AB"}`
That should give you placeholders like [[+user.username]], [[+profile.email]] and [[+customfield]] (no prefix).
The main point of extending the modUser object is to *avoid* using the user extended fields altogether. You create Data fields based on them, then write a quick utility snippet to move the existing values to the data table.
That said, you could try the Profile snippet with &useExtended. The Peoples extra might also be an option.
-
☆ A M B ☆
- 24,524 Posts
Bob, we're talking about an extended user class, and how to use the getExtUser snippet that comes with it, using its &where= property.
Sorry, I was temporarily brain-dead.
Here's one way to do it (not the fastest, but probably fast enough):
$output = '';
$csaacP = "AB";
$csaacmember = $modx->getCollection('modUserProfile',array('state'=>$csaacP));
foreach($csaacmember as $Pmembers) {
/* get user ID from profile object */
$userId = $Pmembers->get('internalKey');
/* get extended fields */
$data = $modx->getObject('userData', array('userdata_id' => $userId);
$fields = array_merge ($Pmembers->toArray(), $data->toArray() );
/* Set $fields['id'] to true user ID */
$fields['id'] = $userId;
$output .= $modx->getChunk('UsersProvTpl', $fields);
}
return $output;
discuss.answer
another way with migxLoopCollection - snippet could be:
[[migxLoopCollection?
&classname=`modUserProfile`
&joins=`[{"alias":"Data","classname":"userData","on":"Data.userdata_id=modUserProfile.internalKey"}]`
&where=`{"modUserProfile.state":"AB"}`
&tpl=`UsersProvTpl`
]]
In your chunk, you would have all UserProfile - fields and and all userdata-fields prefixed with 'Data_' in this case.
That's really nice Bruno. It should be much faster than my version since yours will make one query to the DB and mine will make one for each user.
I was considering doing something similar with getCollectionGraph() but was too lazy to work it out.
Thanks guys! Once again you have saved the day. I knew if anyone could help it would be one of you.
Cheers
Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.