We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27519
    • 275 Posts
    Hi,

    Taking modExtra as a starting point, I have a question about building the list of objects to display in the grid.

    My situation requires two instances for each row in the list: a (primary) tariff object and an (associated) property object.

    I have created the schema like so:
    	<object class="Tariff" table="tariffs" extends="xPDOSimpleObject">
    		<field key="date_from" dbtype="date" precision="10" phptype="string" null="false" default="" />
    		<field key="date_to" dbtype="date" precision="10" phptype="string" null="false" default="" />
    		<field key="tariff" dbtype="smallint" precision ="6" phptype="integer" null="false" default="" />
    		<field key="tariff3" dbtype="smallint" precision ="6" phptype="integer" null="false" default="" />
    		<field key="tariff4" dbtype="smallint" precision ="6" phptype="integer" null="false" default="" />
    		<field key="tariff5" dbtype="smallint" precision ="6" phptype="integer" null="false" default="" />
    		<field key="tariff6" dbtype="smallint" precision ="6" phptype="integer" null="false" default="" />
    		<field key="property" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
    		<aggregate alias="property" class="Property" local="property" foreign="id" cardinality="one" owner="foreign" />
        </object>
        
       	<object class="Property" table="properties" extends="xPDOSimpleObject">
    		<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="address" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    		<field key="postcode" dbtype="varchar" precision="2" phptype="string" null="false" default="" />
    		<field key="town" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
    	</object>
    


    Now in getlist.php (in the processors subdirectory) the list is created which is passed to the grid for display (I removed the query code, which produces the result array of $tariffs):

    foreach ($tariffs as $tariff) {
        $tariffArray = $tariff->toArray();
        $tariffArray['menu'] = array();
        $tariffArray['menu'][] = array(
            'text' => 'Update Tariff',
            'handler' => 'this.updateTariff',
        );
        $tariffArray['menu'][] = '-';
        $tariffArray['menu'][] = array(
            'text' => 'Remove Tariff',
            'handler' => 'this.removeTariff',
        );
    
        $list[]= $tariffArray;
    }
    


    What I’m struggling with is how to get the aggregate instances of "property" in each instance of "tariff" in such a way that it is accessible for the grid object to display.

    I experimented by adding:

    	$property = $tariff->getOne("property");
    


    ... to the above foreach loop which neatly delivers the correct instance of $property. I suppose I could then add the relevant data from $property into $tariffArray.

    But is that the proper way of doing it?
      MODx Revolution / MAMP / OS X
      • 3749
      • 24,544 Posts
      What you’re doing looks fine to me, but it would be more efficient to use getObjectGraph() which would get the object and the related object with a single query. Here’s an example using the user and user profile:

      $user = $modx->getObjectGraph('modUser', array('Profile' =>array()), array('username'=> 'JoeBlow', false))'
      
      /* JSON form equivalent to above . . .
      $user = $modx->getObjectGraph('modUser', '{"Profile":{}}', array('username' => 'JoeBlow'));
      */
      $output[] = $modx->getChunk('userTpl', array_merge($user->toArray(), $user->Profile->toArray()));
      


      The user info is in the $user variable. The Profile stuff is in $user->Profile. The final argument is the cache flag (true/false), which determines whether the result is cached.

      You can also use getCollectionGraph() to get all of them in one go.

      $tariffs = $modx->getCollectionGraph('Tariff', array('Property' =>array() ) );



        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 27519
        • 275 Posts
        Excellent! Works a charme!!

        Thanks for the help....
          MODx Revolution / MAMP / OS X