Attempting to render this on the frontend with migxLoopCollection
works, but only includes the first Option for each Product.
I expected to be able to list multiple options, and for these to be returned as a JSON string or similar.
Actual pseudo output is
- Product 1 name, option_name 1
- Product 2 name, option_name 1
- Product 3 name, option_name 1
Expected pseudo output is
-
Product 1 name, option_name 1
-
Product 1 name, option_name 2
-
Product 2 name, option_name 1
-
Product 2 name, option_name 2
-
Product 3 name, option_name 1
-
Product 3 name, option_name 2
I then expected to be able to format the options JSON string as part of the &tpl
template with a custom snippet.
The migxLoopCollection call I’m using is
[[!migxLoopCollection?
&packageName=`catalog`
&classname=`product`
&joins=`[{"alias":"option","selectfields":"name,price"}]`
&where=`{"published":"1","option.published":"1"}`
&tpl=`migx-loop-row.tpl`
&debug=`1`
]]
Debug shows the SQL query generated is
SELECT `product`.`id`, `product`.`category`, `product`.`name`, `product`.`colour`, `product`.`description`, `product`.`image`, `product`.`deleted`, `product`.`published`, `product`.`pos`, `product`.`createdon`, `product`.`createdby`, `product`.`editedon`, `product`.`editedby`, `option`.`name` AS `option_name`, `option`.`price` AS `option_price` FROM `rap2_catalog_products` AS `product` LEFT JOIN `rap2_catalog_options` `option` ON `product`.`id` = `option`.`rel_product_id` WHERE ( `product`.`published` = 1 AND `option`.`published` = 1 )
When I run this query directly on the database the dataset returned contains all the options as expected - each product has 2x options.
I can’t see anything in the Tutorials in the Docs that indicates where I’ve gone wrong but suspect an extra argument needs passed to migxLoopColletion
.
Can anyone advise what needs done to extract the related product options?
Thanks
Package schema is below for reference.
<?xml version="1.0" encoding="UTF-8"?>
<model package="catalog" baseClass="xPDOObject" platform="mysql" defaultEngine="INNODB" version="1.1">
<!-- define object
- prefix tables with package name to group and separate them from modx core tables or tables created by other packages
-->
<object class="product" table="catalog_products" extends="xPDOSimpleObject" >
<!-- available fields
- category
- name
- colour
- description
- image
-->
<field key="category" dbtype="varchar" precision="255" phptype="string" null="false" default="" index="index" />
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="colour" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="description" dbtype="text" phptype="string" index="fulltext" />
<field key="image" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<!-- used by MIGX -->
<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="pos" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
<!-- good practice for tracking -->
<field key="createdon" dbtype="datetime" phptype="datetime" null="true"/>
<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="editedon" dbtype="datetime" phptype="datetime" null="true"/>
<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<!-- link related tables
- base table (catalog_products) to subordinate table (catalog_options)
- note 'id' is created automatically
-->
<composite alias="option" class="option" local="id" foreign="rel_product_id" cardinality="many" owner="local" />
<aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/>
<aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/>
</object>
<!-- define object
- prefix tables with package name to group and separate them from MODX core tables or tables created by other packages
-->
<object class="option" table="catalog_options" extends="xPDOSimpleObject">
<!-- available fields
- rel_product (catalog_products.id)
- name (dimensions)
- price
-->
<!-- related core table id -->
<field key="rel_product_id" dbtype="int" precision="11" phptype="integer" null="false" default=""/>
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="price" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<!-- used by MIGX -->
<field key="deleted" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="pos" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
<!-- good practice for tracking -->
<field key="createdon" dbtype="datetime" phptype="datetime" null="true"/>
<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<field key="editedon" dbtype="datetime" phptype="datetime" null="true"/>
<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
<!-- link related tables
- subordinate table (catalog_options) back to base table (catalog_product)
- note 'id' is created automatically
-->
<aggregate alias="product" class="product" local="rel_product_id" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/>
<aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/>
</object>
</model>