We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1778
    • 659 Posts
    Hello

    In a CMP I need to display in a grid datas from 2 tables.
    Here a simplified schema :
    <?xml version="1.0" encoding="UTF-8"?>
    <model package="cispeo" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" phpdoc-package="cispeo">
    		
        <object class="cpoCompany" table="cispeo_companies" extends="xPDOSimpleObject">
            
    		<field key="name" dbtype="varchar" precision="80" phptype="string" null="false" default="" index="index" />
    		<field key="office" dbtype="varchar" precision="80" phptype="string" null="true" default="" index="index" />
    		<field key="description" dbtype="text" phptype="text" null="true" default="" />
    	
    		<composite alias="Projects" class="cpoProject" local="id" foreign="owner" cardinality="many" owner="local" />
        </object>
        <object class="cpoProject" table="cispeo_projects" extends="XPDOSimpleObject">
    		<field key="owner" dbtype="int" precision="10" phptype="integer" attributes="unsigned" null="false" default="0" index="index" />
    		<field key="name" dbtype="varchar" precision="50" phptype="string" null="false" default="" index="index" />
    		<field key="description" dbtype="text" phptype="text" null="true" default="" />
    		<field key="status" dbtype="tinyint" precision="1" phptype="integer" attributes="unsigned" null="false" default="0" index="index" />
    		
    		<aggregate alias="Company" class="cpoCompany" local="owner" foreign="id" cardinality="one" owner="foreign" />
    		
    	</object>
    	
    	<!-- An Event -->
    </model>
    


    In my grid ’projects’ (listing all projects cpoProjects) I’d like to display

    ID (project’s ID) | Owner (Company’s ID) | Owner’s Name and office | name (of the project) | ...

    I can’t figure how to display the right values in the "red" column. I need some help and explanations on this... Thanks in advance
    Cheers
      • 27519
      • 275 Posts
      Hi,

      I had a comparable situation with Tariff objects and Property objects, where each Tariff object points to one Property object. The grid with the list of Tariff objects should show the name of the property.

      In the getList.php to fill the grid I did something like this, which worked for me:

      $tariffs = $modx->getCollection('Tariff', $c);
      /* iterate */
      $list = array();
      foreach ($tariffs as $tariff) {
          $property = $tariff->getOne("property");
          $tariffArray = $tariff->toArray();
          $tariffArray["property"] = $property->get("name");
          $list[]= $tariffArray;
      }
      


      In the above I iterate through a collection of Tariff objects which are converted to an array.
      For each Tariff object I retrieve a Property object and add its name to the array that holds the Tariff data.

      Hope this is of use to you.
        MODx Revolution / MAMP / OS X
        • 1778
        • 659 Posts
        Hello
        I’ll try, thanks for the posted code and the explanations smiley
        Cheers

        EDIT : It works fine thanks a lot for your help