We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • How are you instantiating xPDO? If you look at the MODx __construct() method, you can see several options being passed to the parent constructor...
                    array (
                        XPDO_OPT_CACHE_PATH => $cachePath,
                        XPDO_OPT_TABLE_PREFIX => $table_prefix,
                        XPDO_OPT_HYDRATE_FIELDS => true,
                        XPDO_OPT_HYDRATE_RELATED_OBJECTS => true,
                        XPDO_OPT_HYDRATE_ADHOC_FIELDS => true,
                        XPDO_OPT_LOADER_CLASSES => array('modAccessibleObject'),
                        XPDO_OPT_VALIDATOR_CLASS => 'validation.modValidator',
                        XPDO_OPT_VALIDATE_ON_SAVE => true,
                        'cache_system_settings' => true
                    ),
    

    In particular, to access the related objects directly from the parent object by their alias, you need the XPDO_OPT_HYDRATE_RELATED_OBJECTS => true attribute.
      • 11793
      • 49 Posts
      It worked. I added this line immediately after the XPDO object is created:
      $xpdo->config = array_merge($xpdo->config, array(XPDO_OPT_HYDRATE_FIELDS => true,
                          XPDO_OPT_HYDRATE_RELATED_OBJECTS => true));

      Not elegant, but documentation on the xpdo->config property is thin on the ground!

      So, now I can get a single record from two joined tables.

      My next big adventure will involve getCollectionGraph (cue scream SFX shocked).

      Thank you Jason!
        • 11793
        • 49 Posts
        I’m trying to link three tables together with getObjectGraph: WebUsers, WebAttributes and Roles (ignore the fact that Roles is not useed for WebUsers - it’s in the database so I’m taking advantage of it.)

        This works:
        $WebUsers = $this->xpdo->getObjectGraph('WebUsers','{"Profile":{}}',$userId);
        echo $WebUsers->Profile->getOne('Role')->toArray();

        But I’d like to get all related objects in one go. Eventually I will need 4 or more related tables at a time. This does not work:
        $WebUsers = $this->xpdo->getObjectGraph('WebUsers','{"Profile":{"Role":{}}}',$userId);
        echo $WebUsers->Role->toArray();
        

        I understand that Role is in fact related to WebUserAttribute (Profile) and not WebUser. Role is WebUsers’ ... step-brother?

        Here’s the XML of the relationships:
        <object class="WebUsers" table="web_users" extends="xPDOSimpleObject">
        		<composite alias="Profile" class="WebUserAttributes" local="id" foreign="internalKey" cardinality="one" owner="local" />
        		<composite alias="CoProfile" class="CoUserProfiles" local="id" foreign="webuser_id" cardinality="one" owner="local" />
        		
        	</object>
        	<object class="WebUserAttributes" table="web_user_attributes" extends="xPDOSimpleObject">
        		<composite alias="WebUser" class="WebUsers" local="internalKey" foreign="id" cardinality="one" owner="foreign" />
        		<composite alias="Role" class="UserRoles" local="role" foreign="id" cardinality="one" owner="local" />
        	</object>
        	<object class="UserRoles" table="user_roles" extends="xPDOSimpleObject">
        		<composite alias="Profile" class="WebUserAttributes" local="id" foreign="role" cardinality="one" owner="foreign" />
        	</object>
        
        


        My question: is it possible to get objects not directly related using getObjectGraph? That is, a relative of a relative?
        • For anyone participating in this thread, the xPDO documentation has been pretty thoroughly stubbed out at this point. Not sure they will answer these questions directly, but please help us improve the documentation by taking a peek and making suggestions:

          http://svn.modxcms.com/docs/display/XPDO10/Home
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • Quote from: hardboiled at Jul 17, 2009, 07:12 AM

            My question: is it possible to get objects not directly related using getObjectGraph? That is, a relative of a relative?
            No, it has to follow the graph, which is defined by the relationships. If you want to populate specific objects that are not directly related, you can write a manual query and hydrate the objects directly. But for the case you are looking at, you must access the Role through it’s proper relationship, i.e.
            $WebUsers->Profile->Role->toArray()
              • 11793
              • 49 Posts
              Ryan, Jason, thanks for your help. I wasn’t aware of that documentation link - I’ve been using xpdo.org exclusively up until now.