We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14107
    • 21 Posts
    Hello, I wonder if someone could help me with a problem. I’m trying to use getCollectionGraph and keep getting an error, which I don’t understand. Basically, I am trying to show a filename from an Images table, related to an item in an item table. My schema is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="items" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    	<object class="Category" table="category" extends="xPDOSimpleObject">
    		<field key="category" dbtype="varchar" precision="50" phptype="string" null="false" />
            <composite alias="Item" class="Item" local="id" foreign="category_id" cardinality="many" owner="local" />
    	</object>
    	<object class="Images" table="images" extends="xPDOSimpleObject">
    		<field key="item_id" dbtype="int" precision="11" phptype="integer" null="false" />
    		<field key="filename" dbtype="varchar" precision="50" phptype="string" null="false" />
    		<field key="caption" dbtype="varchar" precision="255" phptype="string" null="true" />
            <aggregate alias="Item" class="Item" local="item_id" foreign="id" cardinality="one" owner="foreign" />
    	</object>
    	<object class="Item" table="item" extends="xPDOSimpleObject">
    		<field key="code" dbtype="varchar" precision="50" phptype="string" null="false" />
    		<field key="description" dbtype="text" phptype="string" null="false" />
    		<field key="category_id" dbtype="int" precision="11" phptype="integer" null="false" />
    		<field key="featured" dbtype="varchar" precision="10" phptype="string" null="false" />
            <aggregate alias="Category" class="Category" local="category_id" foreign="id" cardinality="one" owner="foreign" />	
    		<composite alias="Images" class="Images" local="id" foreign="item_id" cardinality="many" owner="local" />
    	</object>
    </model>


    My snippet is

    <?php
    $path = MODX_CORE_PATH . 'components/items/';
    $modx->setOption(xPDO::OPT_HYDRATE_RELATED_OBJECTS, true);
    $modx->addPackage('items',$path . 'model/','brox_');
    
    $c = $modx->newQuery('Item');
    $c->sortby('code','DESC');
    $c->where(array(category_id => $_GET['category_id']));
    
    $items = $modx->getCollectionGraph('Item', '{"Images":{}}', $c );
    
    foreach($items as $item) {
    
       $itemArray = $item->toArray();
       $imageArray = $item->Images->toArray();
       $resultArray = $itemArray + $imageArray;
       $output .= $modx->getChunk($tpl,$resultArray);
    
    }
    
    return $output;



    The error is see is:

    Fatal error: Call to a member function toArray() on a non-object in /home/johnwilk/public_html/core/cache/elements/modsnippet/48.include.cache.php on line 20

    Can anyone point me in the right direction please?
      • 22303 MODX Staff
      • 10,725 Posts
      $item->Images->toArray(); 

      This is wrong. $item->Images is an array of Images and there is no method called toArray() available from a PHP array. You’ll have to iterate the Images as you are the Items... wink
        • 14107
        • 21 Posts
        Hi thanks for your answer. I’m not sure I explained what I’m trying to do well enough. I think what I’m really after is an innerjoin between items and images, so I can get the thumbnail filename for an item. I thought getCollectionGraph helped with this (but I could be wrong).

        I did a similar thing when I wanted the category name (using the same schema) to show in the items list. The code for that, which seems to work, and is almost identical to what I posted earlier is below
        <?php
        $path = MODX_CORE_PATH . 'components/items/';
        $modx->setOption(xPDO::OPT_HYDRATE_RELATED_OBJECTS, true);
        $modx->addPackage('items',$path . 'model/','brox_');
        
        $c = $modx->newQuery('Item');
        $c->sortby('code','DESC');
        $c->where(array(category_id=>$_GET['category_id']));
        
        $items = $modx->getCollectionGraph('Item', '{"Category":{}}', $c );
        
        foreach($items as $item) {
        
           $itemArray = $item->toArray();   
           $categoryArray = $item->Category->toArray();
           $resultArray = $itemArray + $categoryArray;
           $output .= $modx->getChunk($tpl,$resultArray);
        
        }
        
        return $output;

          • 4172
          • 5,888 Posts
          Category has cardinality="one" , Images has cardinality="many"

          what means each Item can have one Category, but many images.

          One Category is an object, but many Images is an array of objects.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 14107
            • 21 Posts
            OK - thanks guys for your help. I’ll try to figure it out. I’m still learning and loving MODX! smiley