We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11793
    • 49 Posts
    I’m trying to use getObjectGraph to populate a MakeTable instance.

    $webUsers = $xpdo->getCollectionGraph('modWebUser',array('modWebUserProfile'=>array('internalKey'=>array())));

    As you can see, I’m linking the WebUser and Attributes tables together.

    However, the table I end up with only has fields from WebUser - the primary table. None of the fields from modWebUserProfile come through. When I print_r the collection graph, the modWebUserProfile fields are in the object:
    [modWebUserProfile] => Array
                                    (
                                        [package] => modx
                                        [table] => web_user_attributes
                                        [fields] => Array
                                            (
                                                [internalKey] => 0
                                                [fullname] => 
                                                [role] => 0
                                                [email] => 
                                                [phone] => 
                                                [mobilephone] => 
                                                [blocked] => 0
                                                [blockeduntil] => 0
                                                [blockedafter] => 0
                                                [logincount] => 0
                                                [lastlogin] => 0
                                                [thislogin] => 0
                                                [failedlogincount] => 0
                                                [sessionid] => 
                                                [dob] => 0
                                                [gender] => 0
                                                [country] => 
                                                [state] => 
                                                [zip] => 
                                                [fax] => 
                                                [photo] => 
                                                [comment] => 
                                            )

    So the million dollar question is: how do I get those related fields to appear as table columns?
    • Is this in context of a model you created for MODx 0.9.6/1.0? modWebUser and modWebUserProfile are not part of Revolution any longer (there are just modUser and modUserProfile as there is no distinction between web and mgr users any longer).

      Also, the graph should only contain classes that are related, i.e. internalKey is invalid in that graph. The relationship of modUser.id to modUserProfile.internalKey must be defined in the model.
        • 11793
        • 49 Posts
        I’m currently using xPDO 1.0 beta 290 on top of classic MODx 0.9.6.3. (On Apache 2.2 + PHP 5 on Windows FYI). The XML file is the one from this topic: http://modxcms.com/forums/index.php/topic,25021.0.html plus some extra tables, of course.

        I’m trying to create a CRUD interface that ties together these tables: WebUser, WebUserAttributes and WebUserAttributesExtended. Here are the relationships between the three tables (minus the fields):

        <object class="modWebUser" table="web_users" extends="modUser">
                <composite alias="modWebUserProfile" class="modWebUserProfile" key="id" local="id" foreign="internalKey" cardinality="one" />
                <composite alias="modWebUserSetting" class="modWebUserSetting" key="id" local="id" foreign="webuser" cardinality="one" />
                <composite alias="modWebGroupMember" class="modWebGroupMember" key="id" local="id" foreign="webuser" cardinality="one" />
                <composite alias="modWebUserAttributesExtended" class="modWebUserAttributesExtended" key="id" local="id" foreign="member_id" cardinality="one" />
            </object>
        <object class="modWebUserProfile" table="web_user_attributes" extends="xPDOSimpleObject">
         <aggregate alias="modWebUser" class="modWebUser" key="internalKey" local="internalKey" foreign="id" cardinality="one" />
                <aggregate alias="modUserRole" class="modUserRole" key="role" local="role" foreign="id" cardinality="one" />
            </object>
         <object class="modWebUserAttributesExtended" table="web_user_attributes_extended" extends="xPDOSimpleObject">
        <aggregate alias="modWebUser" class="modWebUser" key="member_id" local="member_id" foreign="id" cardinality="one" />
        	</object>
        

        modWebUserAttributesExtended is basically doing the same job as modWebUserProfile - just a bunch of extra fields. I took the idea from WebLoginPE, rather than add fields to modWebUserProfile . It is a 1-1 link. And yes, I linked Roles to the WebUser table since I wanted that functionality for regular web users.

        I understand that the key attribute is now deprecated and the "owner" attribute is now in effect. Is that what’s wrong here?

        Can anyone lend a hand regarding what the collection graph + criteria code would be?

        --- MEANWHILE, BACK AT THE RANCH... ---

        OK here’s what I think is going on. I can get a primary table (e.g. modWebUser) and two related tables (modWebUserProfile, modWebUserAttributesExtended) but as you can see from my first post, only the primary table has values. Both of the other two tables have only their default values. I may be completely off base here but I thought that xPDO could do joins? The xPDOQuery object seems to support joins (http://www.xpdo.org/api/xpdo-om/xPDOQuery.html#methodjoin) but there is talk of "See the XPDO_SQL_JOIN constants"; where is this list of constants? The join I’m attempting is actually really simple:
        WHERE modWebUser.id = modWebProfile.internalKey AND modWebUser.id = modWebUserAttributesExtended.member_id

        And before anyone asks, I’ve also tried using a hard-coded SQL statement in a Criteria. That didn’t even get me this far.

        Help me, OpenGeek! You’re my only hope!
          • 11793
          • 49 Posts
          I ended up using the built-in MODx DBAPI calls to create a custom SQL recordset. I then iterated through all records to extract each row’s values into an associative array, which I fed into MakeTable. Inefficient? Tell me about it.

          It was nice to have an alternative solution but I am disappointed that I couldn’t get XPDO to do a simple join.
          • Quote from: hardboiled at Jul 08, 2009, 11:11 AM

            I ended up using the built-in MODx DBAPI calls to create a custom SQL recordset. I then iterated through all records to extract each row’s values into an associative array, which I fed into MakeTable. Inefficient? Tell me about it.

            It was nice to have an alternative solution but I am disappointed that I couldn’t get XPDO to do a simple join.
            It definitely does joins fine; not sure exactly what your problem was, but I suspect the model is the problem and owner is definitely now the important attribute for relationships (key was deprecated and is no longer used). I’ll try and get a new model for Evolution cranked out that addresses this. You may also want to get the latest xPDO version from SVN.
              • 11793
              • 49 Posts
              Thanks Jason, I’ve got the SVN version running fine - no errors on my existing code.

              I’ll go thru the XML with a fine-tooth comb and let you know if getCollectionObject ends up working.

              I was thinking that I only need to check the tables that my code references - I don’t need to set up links for every single table in MODx do I?
              • Quote from: hardboiled at Jul 09, 2009, 05:36 AM

                Thanks Jason, I’ve got the SVN version running fine - no errors on my existing code.

                I’ll go thru the XML with a fine-tooth comb and let you know if getCollectionObject ends up working.

                I was thinking that I only need to check the tables that my code references - I don’t need to set up links for every single table in MODx do I?
                Right, as long as the relationships you want to actually use in the graph is there, you should be fine. No need to define every relationship for every table.
                  • 11793
                  • 49 Posts
                  Here’s a new XML file that I’m working on to get the WebUser and Attributes tables joined:
                  <object class="WebUserAttributes" table="web_user_attributes" extends="xPDOSimpleObject">
                  		<field key="internalKey" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
                  		[ ... fields removed for brevity ... ]
                  		<composite alias="User" class="WebUsers" local="internalKey" foreign="id" cardinality="one" owner="foreign" />
                  	</object>
                  	<object class="WebUsers" table="web_users" extends="xPDOSimpleObject">
                  		<field key="username" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="unique" />
                  		<field key="password" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
                  		<field key="cachepwd" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
                  		<composite alias="Profile" class="WebUserAttributes" local="id" foreign="internalKey" cardinality="one" owner="local" />
                  	</object>

                  Do these two tables have the correct type of link? Are the composite tags correctly attributed?

                  I ask this because the following lines of code all return the WebUsers table only:
                  $WebUsers = $this->xpdo->getObjectGraph('WebUsers','{WebUserAttributes:{Profile:{}}}',1);
                  OR
                  $WebUsers = $this->xpdo->getObjectGraph('WebUsers','{Profile:{internalKey:{}}}',1);
                  THEN
                  echo $WebUsers->toArray();
                  

                  I’m using the latest SVN of XPDO, as of last week, running on classic MODx 0.9.6.3.
                  • The schema looks fine, but the call to the function should simply be:
                    $WebUser = $this->xpdo->getObjectGraph('WebUsers', '{"Profile":{}}', 1);

                    Then to get the data, you still have to look at the child object data on the $WebUser object, i.e.
                    $userData = $WebUser->toArray();
                    $userProfileData = $WebUser->Profile->toArray();
                      • 11793
                      • 49 Posts
                      Thanks for that Jason. My problem now is that I can only access the related Profile object like this:
                      print_r($WebUsers->_relatedObjects['Profile']->toArray());

                      The method you described above (i.e. $WebUser->Profile->toArray() ) does not work - it returns an error:
                      Fatal error: Call to a member function toArray() on a non-object

                      Not a deal-breaker right now, but it is a little confusing. Any thoughts?