GetCollectionGraph and join inside same class

Hello, I’ve got a class, that reffers to self

<object class="Item" table="migx_products_items" extends="xPDOSimpleObject">

        <field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>

        <field key="alias" dbtype="mediumtext" phptype="string"/>

        

        <field key="category" dbtype="int" precision="255" phptype="integer" null="false" default="0" />

       

        <field key="published" dbtype="int" precision="1" attributes="unsigned" phptype="integer" null="false" default="0"/>

        <field key="pos" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0"/>

        <aggregate alias="Category" class="Category" local="category" foreign="id" cardinality="one" owner="foreign"/>

    </object>    


<object class="Category" table="migx_products_categories" extends="xPDOSimpleObject">

            <field key="title" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>

            <field key="alias" dbtype="varchar" precision="255" phptype="string" null="false" default=""/>

            <field key="parent" dbtype="int" precision="10" phptype="integer" null="false" default="0"/>

            <field key="pos" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0"/>

            <aggregate alias="Item" class="Item" local="id" foreign="category" cardinality="many" owner="local"/>

            <aggregate alias="Parent" class="Category" local="parent" foreign="id" cardinality="many" owner="local"/>

        </object>

I’m trying to get data with getCollectionGraph

$items = $this->modx->getCollectionGraph('Item', '{ "Category" : { "Parent" : {} } }', $q);

but I can not find a way to get Parent data

$obj->Category->Parent->get('title');

throws an error get on null

If I don’t include “Parent” in joins

$items = $this->modx->getCollectionGraph('Item', '{ "Category" : { }', $q);

I can easily get it with

$obj->Category->getOne('Parent')->get('title');

Can someone explain why?
Thank you.

You need to show your Item model, as well. It looks like your Item aggregate may not be correct? I’m a bit confused without seeing both classes.

I’ve added it to initial post.

You have the Parent aggregate defined as cardinality=“many”. I don’t think that’s what you meant based on the code you are sharing.

1 Like

Aside from the cardinality, yu’re also defining owner="local" on the Parent relation, however the primary key of the relation is on foreign so that should be foreign.

2 Likes

Thank you, opengeek and markh. It works just fine after these two fixes. I’ve applied both simultaneously, so I don’t know, which one solved the problem (or maybe both), so I’ll mark the first answer as the solution. Thanks again.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.