Extending Modx modResource schema errors

170 Views Asked by At

I'm trying to extend the modx modresource object, but keep getting errors & I can't seem to figure out why. It is related to the schema (I think) but everything looks correct.

Schema:

<?xml version="1.0" encoding="UTF-8"?>

<model package="extresource" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" tablePrefix="modx_" version="1.0.0">
    <object class="extResource" extends="modResource">
        <composite alias="ResourceData" class="ResourceData" local="id" foreign="internalKey" cardinality="one" owner="local"/>
    </object>
    <object class="ResourceData" table="resource_data" extends="xPDOSimpleObject">
        <field key="internalKey" dbtype="int" precision="11" phptype="integer" null="false" attributes="unsigned"/>
        <field key="views" dbtype="int" precision="11" phptype="integer" null="true" />
        <field key="starred" dbtype="int" precision="10" phptype="integer" null="false" />

        <index alias="internalKey" name="internalKey" primary="false" unique="true" type="BTREE" >
            <column key="internalKey" length="" collation="A" null="false" />
        </index>

        <aggregate alias="Resource" class="modResource" local="internalKey" foreign="id" cardinality="one" owner="foreign"/>

    </object>
</model>

I'm testing it using:

$resource = $modx->getObject('modResource', 11112);
echo $resource->get('pagetitle'); //test I have the resource
$data = $resource->getOne('ResourceData');

The errors I get are:

Could not getOne: foreign key definition for alias ResourceData not found. No foreign key definition for parentClass: modDocument using relation alias: ResourceData

The table exists & has data, the package is registered in the modx extension packages. I've been over the schema many times & it looks right.

What is causing these errors?

2

There are 2 best solutions below

0
On

Does the resource you are loading have its class_key field set to extResource? That's needed for it to load the right resource object class.

1
On

You have to use the right object class in $modx->getObject. Otherwise you will get a modResource object, that does not know the extended object data and relationship.

$resource = $modx->getObject('extResource', 11112);