Hi again,
I’m having another problem with getCollectionGraph: I have categories, which contain several products which have several prices for different materials (see schema definition below).
I’d like to fetch a collection graph containing all categories, products and their prices ({ "Products":{ "Prices":{} } }):
<?php
$categories = $modx->getCollectionGraph('Category', '{ "Products":{ "Prices":{} } }');
I can loop through all categories and products but there is always only one price fetched for a product:
<?php
foreach($categories as $category) {
foreach($category->Products as $product) {
echo count($product->Prices); // returns always 0 or 1 even if there are more than 1 prices defined in the DB table
}
}
Am I doing something wrong?
Here my (simplified) schema definition:
<?xml version="1.0" encoding="UTF-8"?>
<model package="products" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="Product" table="product" extends="xPDOSimpleObject">
<field key="id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" generated="native" />
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" index="unique" />
<field key="category" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" />
<composite alias="Prices" class="Price" local="id" foreign="product" cardinality="many" owner="local" />
<aggregate alias="Category" class="Category" local="category" foreign="id" cardinality="one" owner="foreign" />
</object>
<object class="Category" table="category" extends="xPDOSimpleObject">
<field key="id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" generated="native" />
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<composite alias="Products" class="Product" local="id" foreign="category" cardinality="many" owner="local" />
</object>
<object class="Material" table="material" extends="xPDOSimpleObject">
<field key="id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" generated="native" />
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
</object>
<object class="Price" table="price" extends="xPDOObject">
<field key="product" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" />
<field key="material" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="pk" />
<field key="price" dbtype="float" attributes="unsigned" phptype="float" null="false" default="0" />
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
<column key="product" collation="A" null="false" />
<column key="material" collation="A" null="false" />
</index>
<aggregate alias="Product" class="Product" local="product" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Material" class="Material" local="material" foreign="id" cardinality="one" owner="foreign" />
</object>
</model>
Thanks a lot!