Topic: Two questions about object graphs and newQuery  (Read 1206 times)

Pages: [1]   Go Down

#1: 20-Jul-2009, 10:06 PM

hardboiled
Posts: 37

Two questions:

1. Do Object Graphs only support "direct-descendant chaining"? (for want of a better term)

Example
Code:
$WebUsers = $xpdo->getCollectionGraph('WebUsers','{"Profile":{"SubProfile":{}}}');

In the above example, the only way to get the SubProfile table is to set it up as a direct child of Profile (via a composite/aggregate link in the XML). I cannot seem to get SubProfile to load if it is a direct child of WebUsers.

2. Does newQuery only get fields from the 'parent' table?

Example
Code:
$c = $xpdo->newQuery('WebUsers');
$c->innerJoin('WebUserAttributes','Profile');
$WebUsers = $xpdo->getCollection('WebUsers',$c);
When you turn on debug for the above example, you can dig up this query in the resultant noise:
Code:
Attempting to execute query using PDO statement object:
SELECT WebUsers.id, Profile.fullname
FROM `modx_web_users`
AS `WebUsers`
 JOIN `modx_web_user_attributes` `Profile`
ON `WebUsers`.`id` = `Profile`.`internalKey`

This SQL actually works when run straight against the database. However, the result only seems to contain fields from the WebUsers table.

How does one extract the field Profile.fullname from the returned collection?

#2: 21-Jul-2009, 12:32 AM

hardboiled
Posts: 37

Have a look at this syntax:
Code:
$WebUsers = $xpdo->getCollectionGraph('WebUsers','{"Profile":{}, "CoProfile":{}}');
Both Profile and CoProfile are siblings, and children of WebUsers. I'm able to successfully see them in the returned collection.

I guess my point is: I haven't actually seen that syntax anywhere in the XPDO docs. All examples that I've seen suggest a grandparent -> parent -> child hierarchy:
Code:
$xpdo->getCollectionGraph('GrandParent','{"Parent":{ "Child":{}}}')

I'm hoping that others will benefit from my good fortune today.

#3: 21-Jul-2009, 08:12 AM

Moderator

OpenGeek
MODx Co-Founder
Posts: 6,977

damn accurate caricatures...

WWW
2. Does newQuery only get fields from the 'parent' table?

Example
Code:
$c = $xpdo->newQuery('WebUsers');
$c->innerJoin('WebUserAttributes','Profile');
$WebUsers = $xpdo->getCollection('WebUsers',$c);
When you turn on debug for the above example, you can dig up this query in the resultant noise:
Code:
Attempting to execute query using PDO statement object:
SELECT WebUsers.id, Profile.fullname
FROM `modx_web_users`
AS `WebUsers`
 JOIN `modx_web_user_attributes` `Profile`
ON `WebUsers`.`id` = `Profile`.`internalKey`

This SQL actually works when run straight against the database. However, the result only seems to contain fields from the WebUsers table.

How does one extract the field Profile.fullname from the returned collection?
You wouldn't unless you have XPDO_OPT_HYDRATE_ADHOC_FIELDS option set to true; then it would be an "ad-hoc" field on the returned lazy objects.  The other option would be not to use getCollection, avoiding the objects altogether, and instead simply executing the query produced by newQuery, e.g.
Code:
$c = $xpdo->newQuery('WebUsers');
$c->innerJoin('WebUserAttributes','Profile');
$c->prepare();
$stmt = $xpdo->query($c->toSQL());
if ($stmt) {
    $rs = $stmt->fetchAll(PDO_FETCH_ASSOC);
}
Jason Coward
MODx Co-Founder
xPDO Founder
CTO @ Collabpad
work productively.
work intelligently.
work together.
Light is just a vibration of a note too. Everything is. You've got to keep that in mind.
  Frank Zappa

#4: 21-Jul-2009, 08:19 AM

Foundation

splittingred
Posts: 1,520

i am alt-country rock

WWW
2. Does newQuery only get fields from the 'parent' table?
Yes, by default.

Quote
How does one extract the field Profile.fullname from the returned collection?

Code:
$c = $xpdo->newQuery('WebUsers');
$c->select('WebUsers.*, Profile.fullname AS fullname');
$c->innerJoin('WebUserAttributes','Profile');
$webusers = $xpdo->getCollection('WebUsers',$c);
foreach ($webusers as $user) {
   echo $user->get('fullname');
}
shaun mccormick | modx foundation
modx revolution | jira bugtracker | official docs | svn tracker | api docs
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.