MODX can't find custom class when extending it

Hi all,

I’m trying to extend a custom class by using inherit="single" in the db schema:

    <object class="earthImage" table="earthbrain_images" extends="xPDOSimpleObject" inherit="single">
        <field key="class_key" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
        <field key="object_id" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="" />
        ...bunch of other stuff...
    </object>
    <object class="earthSeedImage" extends="earthImage">
        <aggregate alias="Seed" class="earthSeed" local="object_id" foreign="id" cardinality="one" owner="foreign" />
    </object>

The classes are created automatically by GPM, and this is inside my base class:

    function __construct(modX &$modx, array $config = [])
    {
        $this->modx =& $modx;

        $corePath = $this->modx->getOption('earthbrain.core_path', $config, $this->modx->getOption('core_path') . 'components/earthbrain/');
        $assetsUrl = $this->modx->getOption('earthbrain.assets_url', $config,$this->modx->getOption('assets_url') . 'components/earthbrain/');

        $this->config = array_merge([
            'basePath' => $this->modx->getOption('base_path'),
            'corePath' => $corePath,
            'modelPath' => $corePath . 'model/',
            'processorsPath' => $corePath . 'processors/',
            'jsUrl' => $assetsUrl . 'js/',
            'cssUrl' => $assetsUrl . 'css/',
            'assetsUrl' => $assetsUrl,
            'connectorUrl' => $assetsUrl . 'connector.php',
        ], $config);

        $this->modx->loadClass('earthImage', $this->config['modelPath'] . 'earthbrain/');
        $this->modx->loadClass('earthSeed', $this->config['modelPath'] . 'earthbrain/');
        $this->modx->loadClass('earthSeedImage', $this->config['modelPath'] . 'earthbrain/');

        $this->modx->addPackage('earthbrain', $this->config['modelPath']);
    }

When I get an earthImage object, all is well. But when I try to fetch the extended earthSeedImage class, it throws a fatal error saying the earthImage class cannot be found.

I looked closely at the schema of Commerce, where @markh is also extending custom classes. I don’t know what’s going on exactly in the constructor there, with a custom adapter and new MODX instance and Alpackas and such… But I noticed that the classes are loaded there one by one, because apparently inheritance doesn’t work as expected otherwise.


        // Create a new Adapter/Revolution instance. In the future, there should be some logic here to determine what
        // adapter to use for MODX3 or even Evo or other platforms.
        $this->adapter = new Revolution($instance, $this);

        // Load some non-autoloading compatible comSimpleObject as it's a core dependency other objects rely on.
        // If they're not loaded by the adapter/MODX, inheritance doesn't work as expected.
        // This also makes the classes available so things like comOrder::STATE_PROCESSING can be used
        $this->adapter->loadClass('comSimpleObject', $this->config['model_path'] . 'commerce/');
        $this->adapter->loadClass('comAddress', $this->config['model_path'] . 'commerce/');
        $this->adapter->loadClass('comOrder', $this->config['model_path'] . 'commerce/');
        //...and a couple more...

So I tried loading the classes directly in modx.class.php and voila, it works. Why is it not working from my own constructor though? The package is also loaded via extension_packages already.

What am I missing?

The base class indeed needs to be loaded for it to work. The adapter and Alpacka in Commerce is unrelated.

The problem is you’re using model_path here for the config key, but the config defines the path as modelPath instead.

Thanks Mark, stupid mistake, fixed it in the example. I’m afraid it’s not the culprit though… I tried it with fixed config key and hardcoded paths, but same results.

I’m having more issues now related to that class. Functions throw fatal errors too and when I remove the package from extension_packages, MODX just throws me out entirely with fatal MySQL errors in the log. So I’m doing something very wrong :slight_smile:

Never mind, that was a faulty getService call. So the problem still stands.

Doh :man_facepalming: It was the broken getService call all along! I guess that’s why it worked when I placed it in the MODX class.

All good now with fixed getService and correct config keys. Thanks again Mark :slight_smile:

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”.