We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11793
    • 49 Posts
    Two questions:

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

    Example
    $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
    $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:
    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?
      • 11793
      • 49 Posts
      Have a look at this syntax:
      $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:
      $xpdo->getCollectionGraph('GrandParent','{"Parent":{ "Child":{}}}')


      I’m hoping that others will benefit from my good fortune today.
      • Quote from: hardboiled at Jul 21, 2009, 03:06 AM

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

        Example
        $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:
        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.
        $c = $xpdo->newQuery('WebUsers');
        $c->innerJoin('WebUserAttributes','Profile');
        $c->prepare();
        $stmt = $xpdo->query($c->toSQL());
        if ($stmt) {
            $rs = $stmt->fetchAll(PDO_FETCH_ASSOC);
        }
          • 28215
          • 4,149 Posts
          Quote from: hardboiled at Jul 21, 2009, 03:06 AM

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


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

          $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 | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com