-
- 620 Posts
consider this:
$criteria = $this->modx->newQuery('Program');
$criteria->where(array('Program.id' => $program_id));
$criteria->limit(1);
$programs = $this->modx->getCollectionGraph('Program', '{ "Program":{"ProgramName":{}, }', $criteria);
foreach($programs as $program){
/* whatever */
}
I am only expecting one result here, is there a way to access that result without bothering with a loop? I tried to figure out what getCollectionGraph is returning in the docs, but "a collection of objects' is not really super helpful to me.
-thanks
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
-
- 24,544 Posts
You could try dereferencing it with
$program = reset($programs)
but I think you'd only gain a few milliseconds and I'm not sure it would work.
-
- 620 Posts
Quote from: BobRay at Nov 06, 2018, 10:05 PMYou could try dereferencing it with
$program = reset($programs)
but I think you'd only gain a few milliseconds and I'm not sure it would work.
it seems odd to me ... what is get collections actually returning? an array of objects?
*** Not just websites, we also create signage, banners, print, trade show displays and more! ***
Sean Kimball CLP, CLS.
Technical Director / Sr. Developer | BigBlock Studios
._______________________________________________.
Bigblock Studios
http://www.bigblockstudios.ca Web site design & development.
27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
phone/fax: 905-426-5525
-
- 24,544 Posts
It returns an array of objects with the related object fields added as a separate, subsidiary object.
Something like this should work:
$users = $modx->getCollectionGraph('modUser', '{"Profile":{}}', $c);
/* Comes back as a user object with the added member
variable $Profile containing the profile fields */
$user = reset($users);
$fields = $user->toArray();
unset($fields['password'], $fields['cachepwd'], $fields['salt'], $fields['hash_class'] );
if ($user->Profile) {
$fields = array_merge($user->Profile->toArray(), $fields);
}
$userame = $fields['username'];
$email = $fields['email'];
Note that if you need the user's ID, it's overwritten by the profile ID, so you'd have to get it with:
$userId = $fields['internalKey'];