Simple example from modx3 doc with composite key not working

Hey everyone,

I am using ClassExtender to extend modUser, it worked fine on modx2 but not on modx3

[[!ClassExtender?
&package=extendeduser
&schemaTpl=MyExtUserSchema
&tablePrefix=ext_
&dirPermission=0755
]]

I have pasted the “Simple example” schema into MyExtUserSchema.
https://docs.modx.com/3.x/en/extending-modx/custom-users#simple-example:

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

<model package="extendeduser" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" tablePrefix="ext_">

        <!-- extend the modUser class -->

        <object class="extUser" extends="modUser">

                <composite alias="Data" local="id" class="Userdata" foreign="userdata_id" cardinality="one" owner="local" />

        </object>

        <object class="Userdata" table="data" extends="xPDOSimpleObject">

                <field key="userdata_id" dbtype="int" precision="11" phptype="integer" null="false" attributes="unsigned"/>

                <field key="facebook_url" dbtype="varchar" precision="100" phptype="string" null="true" />

                <index alias="userdata_id" name="userdata_id" primary="false" unique="true" type="BTREE">

                    <column key="userdata_id" length="" collation="A" null="false" />

                </index>

                <aggregate alias="extUser" local="userdata_id" foreign="id" cardinality="one" owner="foreign" />

        </object>

 </model>

With modx2 i had no issue, but with modx 3 i get:

createObjectContainer() failed

Error log:
/var/www.mysite.dev/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php
Line 666 - Could not load class: extendeduser\extUser from mysql.extendeduser\extuser

Any help appreciated!
And thank you all for the great job with Modx3!

The schema isn’t valid for MODX 3. You should be using fully qualified class names like:

MODX\\Revolution\\modUser

Example of a valid schema that works for me in 3.0.3-pl:

<?xml version="1.0" encoding="UTF-8"?>
<model package="Emails\Model\" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" version="3.0">
	<object class="News" table="emails_news" extends="xPDO\Om\xPDOSimpleObject">
		<field key="email" dbtype="varchar" precision="100" phptype="string" null="true" />
		<field key="deleted" dbtype="tinyint" precision="1" phptype="integer" null="false" />
		<field key="hash" dbtype="varchar" precision="50" phptype="string" null="false" />
		<field key="created" dbtype="datetime" phptype="datetime" null="false" />
		<field key="updated" dbtype="datetime" phptype="datetime" null="false" />
        <field key="hidden" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" />
	</object>
</model>

EDIT: It could be that ClassExtender converts the schema and the error is unrelated. I’ve only used MIGX parse the schema before.

1 Like

Thank you @excavator! (but unfortunately it does not work… i notice there is no composite key in your schema)

I think the composite key is the reason of my issue
If we take the “Simple example” of the doc, the table is created only after i remove the 3 lines for the composite key

These 3 lines are the culprits in modx3 :

        <object class="extUser" extends="modUser">

                <composite alias="Data" local="id" class="Userdata" foreign="userdata_id" cardinality="one" owner="local" />

        </object>

ClassExtender translates classes for MODX 3, but only for common MODX classes. It may be failing to properly namespace the class in your composite alias, but MODX 3 should do the translation for you for modUser.

Have you looked at the schema file ClassExtender produces in the core/components/model/schema/ directory?

If it’s not correct, edit the chunk, not the file.

1 Like

I haven’t look at ClassExtender for MODX 3 yet, but like others have said, the schemas in MODX 3 look a bit different.

  • The version in the <model> tag is now version="3.0"
  • The fully qualified class names have to be used for classes you extend (e.g. xPDO\Om\xPDOSimpleObject instead of xPDOSimpleObject) and in all the relationships to other tables (e.g. <composite alias="Data" class="Extendeduser\Model\Userdata" ...> instead of <composite alias="Data" class="Userdata" ...>).

So you schema should probably look something like this: (untested)

<?xml version="1.0" encoding="UTF-8"?>
<model package="Extendeduser\Model\" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" tablePrefix="ext_" version="3.0">

    <!-- extend the modUser class -->
    <object class="extUser" extends="MODX\Revolution\modUser">
        <composite alias="Data" class="Extendeduser\Model\Userdata" local="id" foreign="userdata_id" cardinality="one" owner="local" />		       
    </object>

    <object class="Userdata" table="data" extends="xPDO\Om\xPDOSimpleObject">
        <field key="userdata_id" dbtype="int" precision="11" phptype="integer" null="false" attributes="unsigned"/>
        <field key="facebook_url" dbtype="varchar" precision="100" phptype="string" null="true" />

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

        <aggregate alias="extUser" class="Extendeduser\Model\extUser" local="userdata_id" foreign="id" cardinality="one" owner="foreign" />
    </object>

</model>
1 Like

Thank you very much everyone!

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.