MODX Revolution 2.2.6-pl (traditional)
The Solution
Sometimes the best solutions are the ones you discover yourself!
The solution was found on fourth reading of another post https://forums.modx.com/forums/thread/74205/getmany-error-no-foreign-key-definition-for-parentclass#dis-post-411671 and subsequently on Bob's Guides http://bobsguides.com/custom-db-tables.html (where else!)under Related Objects near the end of the article. If you are using the createXpdoClasses build script on Bobs site edit the snippet near line 30 from
$createSchema = $modx->getOption('createSchema',$scriptProperties,true);
to
and after four days of head banging and hair pulling I have that piece of the puzzle in place!
Original Post
Apparently I still haven't mastered basic xPDO yet. I have a database of laboratories and analysts. The abbreviated schema looks like this:
<object class="Analysts" table="analysts" extends="xPDOObject">
<field key="sa_ID" dbtype="int" precision="11" phptype="integer" null="false" index="pk" generated="native" />
<field key="FK_LabID" dbtype="int" precision="11" phptype="integer" null="true" />
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
<column key="sa_ID" length="" collation="A" null="false" />
</index>
<aggregate alias="Labs" class="Labs" local="FK_LabID" foreign="lab_ID" cardinality="one" owner="foreign" />
</object>
<object class="Labs" table="labs" extends="xPDOObject">
<field key="lab_ID" dbtype="int" precision="11" phptype="integer" null="false" index="pk" generated="native" />
<field key="lab_name" dbtype="varchar" precision="255" phptype="string" null="true" />
<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
<column key="lab_ID" length="" collation="A" null="false" />
</index>
<composite alias="Analysts" class="Analysts" local="lab_ID" foreign="FK_LabID" cardinality="many" owner="local" />
</object>
Each laboratory can have many analysts but an analyst works in only one lab; one (laboratory) to Many (Analysts). The snippet I have paraphrased from the examples
http://rtfm.modx.com/display/xPDO20/More+Examples+of+xPDO+XML+Schema+Files returns only the laboratory name and not the expected (7) analysts that work there.
$path = MODX_CORE_PATH . 'components/csaac/';
$result = $modx->addPackage('csaac',$path . 'model/','csaac_');
$output = '';
$labs= $modx->getObject('Labs', array('lab_ID' => 1 ) );
$analyst= $labs->getMany('Analysts');
$output .= $labs->get('lab_name');
foreach ($analyst as $c)
{
$output .= $c->get('Surname').'<br />';
}
return $output;
The same thing occurs when I copy and paste the blogpost example
http://rtfm.modx.com/display/xPDO20/More+Examples+of+xPDO+XML+Schema+Files
into my system.
In both instances I get the same error:
[2013-01-29 13:07:48] (ERROR @ /CSAAC/index.php) No foreign key definition for parentClass: Labs using relation alias:
[2013-01-29 13:08:54] (ERROR @ /CSAAC/index.php) No foreign key definition for parentClass: Blogposts using relation alias: Comments
Clearly something that I can't see is very wrong here. Can SKS point me in the right direction?
Thanks
[ed. note: jimsheppard last edited this post 13 years, 8 months ago.]