Hi @bobray. I just tried getObjectGraph, and I still get the No foreign key definition for parentClass: Product using relation alias: Association
error.
Here’s what I’m using now:
Snippet
<?php
// Package vars
$package = 'Vendx';
$prefix = 'vdx_';
// xPDO
include MODX_CORE_PATH.'config/config.inc.php';
$xpdo = new xPDO($database_dsn, $database_user, $database_password, $table_prefix);
$xpdo->setLogLevel(xPDO::LOG_LEVEL_INFO);
$xpdo->setLogTarget('ECHO');
if (!$xpdo->addPackage($package, MODX_CORE_PATH."components/$package/model/", $prefix)) {
print 'There was a problem adding your package.';
} else {
$productTrans = $xpdo->getObject('ProductTranslation', array('slug' => 'test-product') );
$pid = $productTrans->get('translatable_id');
$product = $xpdo->getCollectionGraph('Product', array('Association' => array('product_id' => $pid)), $pid);
echo '<pre>';
print_r($product);
echo '</pre>';
}
?>
XML
<?xml version="1.0" encoding="UTF-8"?>
<model package="Vendx" baseClass="xPDOObject" platform="mysql" defaultEngine="InnoDB" version="1.1">
<object class="Product" table="product" extends="xPDOSimpleObject">
<field key="main_taxon_id" dbtype="int" phptype="integer" null="true" index="index" />
<field key="code" dbtype="varchar" precision="255" phptype="string" null="false" index="unique" />
<field key="created_at" dbtype="datetime" phptype="datetime" null="false" />
<field key="updated_at" dbtype="datetime" phptype="datetime" null="true" />
<field key="enabled" dbtype="tinyint" precision="1" phptype="integer" null="false" />
<field key="variant_selection_method" dbtype="varchar" precision="255" phptype="string" null="false" />
<field key="average_rating" dbtype="double" phptype="float" null="false" default="0" />
<index alias="UNIQ_677B9B7477153098" name="UNIQ_677B9B7477153098" primary="false" unique="true" type="BTREE" >
<column key="code" length="" collation="A" null="false" />
</index>
<index alias="IDX_677B9B74731E505" name="IDX_677B9B74731E505" primary="false" unique="false" type="BTREE" >
<column key="main_taxon_id" length="" collation="A" null="true" />
</index>
<composite alias="Association" class="ProductAssociation" local="id" foreign="product_id" cardinality="many" owner="local" />
</object>
<object class="ProductAssociation" table="product_association" extends="xPDOSimpleObject">
<field key="association_type_id" dbtype="int" phptype="integer" null="false" index="index" />
<field key="product_id" dbtype="int" phptype="integer" null="false" index="index" />
<field key="created_at" dbtype="datetime" phptype="datetime" null="false" />
<field key="updated_at" dbtype="datetime" phptype="datetime" null="true" />
<index alias="product_association_idx" name="product_association_idx" primary="false" unique="true" type="BTREE" >
<column key="product_id" length="" collation="A" null="false" />
<column key="association_type_id" length="" collation="A" null="false" />
</index>
<index alias="IDX_48E9CDABB1E1C39" name="IDX_48E9CDABB1E1C39" primary="false" unique="false" type="BTREE" >
<column key="association_type_id" length="" collation="A" null="false" />
</index>
<index alias="IDX_48E9CDAB4584665A" name="IDX_48E9CDAB4584665A" primary="false" unique="false" type="BTREE" >
<column key="product_id" length="" collation="A" null="false" />
</index>
<aggregate alias="Product" class="Product" local="product_id" foreign="id" cardinality="one" owner="foreign" />
</object>
</model>
Even with this simplified to the above code I still get that foreign key error when using any call to getOne, getMany, getObjectGraph, or getCollectionGraph, and their respective key => value array pointers.
The Product object does get retrieved, just not getting any relational data with it.
I realized I was previously missing the opening <model>
element in the XML, but have added it as shown above before producing the noted error. I’ve cleared the cache to be sure.
The only thing that works without producing the foreign key relation error is getObject, or getCollection.
This foreign key exists in the MySQL database but has a constraint name of vdx_product_association_ibfk_1. Should that be changed to the alias Association
like in the XML file?
Is there some other place I need to tell MODX that I’m using these foreign keys?