MODX 3 custom tables 'class not found' [Solved]

Hi,
I’m currently adding custom tables to MODX 3 which seems to be a bit different than with MODX 2. according to Using Custom Database Tables - Tutorials | MODX Documentation

I’ve adjusted the folders/locations accordingly and I’ve changed the xml schema with namespaces etc. When building the Schema, the Modal folder is added to the src folder with all classes in it, so that seems to be ok. But it looks like the namespace is not adding the classname.
Below the cdbTeam.php file from the Model dir. In the docs it says the namespace should be something like ‘namespace CustomDatabase\Model\cdbTeam’

<?php
namespace CustomDatabase\Model;

use xPDO\xPDO;

/**
 * Class cdbTeam
 *
 * @property string $naam
 * @property string $teamtype
 * @property float $lat
 * @property float $lng
 * @property integer $radius
 * @property integer $aantal_bomen
 * @property integer $landschap_id
 * @property string $createdon
 * @property integer $createdby
 * @property string $editedon
 * @property integer $editedby
 *
 * @package CustomDatabase\Model
 */
class cdbTeam extends \xPDO\Om\xPDOSimpleObject
{
}

When I try to create the tables I get an error that the classes don’t exist.
(class_exists(“Customdatabase\Model\cdbTeam”) always returns ‘false’.

Any ideas?

If needed, the xml begins like this:

<?xml version="1.0" encoding="utf-8"?>
<model package="CustomDatabase\Model" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="MyISAM"  version="3.0">
	
	
	<!-- TABEL cdbTeam -->
	<object class="cdbTeam" table="cdb_team" extends="xPDO\Om\xPDOSimpleObject">
		<field key="naam" dbtype="varchar" precision="100" phptype="string" null="true" default="" />
		<field key="teamtype" dbtype="varchar" precision="100" phptype="string" null="true" default="vast" /><!--vast/flexibel-->
		
		<field key="lat" dbtype="float" precision="16,14" phptype="float" null="true" default="" />
		<field key="lng" dbtype="float" precision="16,14" phptype="float" null="true" default="" />	
		
		<field key="radius" dbtype="int" precision="1" phptype="integer" null="false" default="5" /><!--kms rond locatie-->
		<field key="aantal_bomen" dbtype="int" precision="1" phptype="integer" null="false" default="5" />
		<field key="landschap_id" dbtype="int" precision="1" phptype="integer" null="false" default="0" />
		
		
		<field key="createdon" dbtype="datetime" phptype="datetime" null="true"/>
		<field key="createdby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
		<field key="editedon" dbtype="datetime" phptype="datetime" null="true"/>
		<field key="editedby" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
		
		<aggregate alias="Landschap " class="CustomDatabase\Model\cdbLandschap" local="landschap_id" foreign="id" cardinality="one" owner="foreign"/>
		
		<aggregate alias="CreatedBy " class="MODX\Revolution\modUser" local="createdby" foreign="id" cardinality="one" owner="foreign"/>
		<aggregate alias="EditedBy " class="MODX\Revolution\modUser" local="editedby" foreign="id" cardinality="one" owner="foreign"/>
	</object>

No, I think it is correct the way it is now.


How exactly do you try to create the tables?
Do you call $modx->addPackage() for your model before executing createObjectContainer()?
Or do you have a bootstrap.php file that calls $modx->addPackage()?


If you can’t make it work, then maybe use the extra ExtraBuilder instead.

Thanks for the reply.

I’ll have a look at the extrabuilder, but I’d like to know how it works because I have a couple of websites with custom tables that need to be updated to MODX 3.

The addPackage() is called in the bootstrap.php indeed:

$modx->addPackage('CustomDatabase\Model', $namespace['path'] . 'src/', null, 'CustomDatabase\\');

The error log is empty, so it’s hard to tell what’s going wrong.

Found out what was wrong, typo :unamused:

“Customdatabase\Model\cdbTeam” had to be “CustomDatabase\Model\cdbTeam”

I find the PhpStorm editor helpful in these kind of situations. Other good code editors may be as well.

MODX 3 is a project in PhpStorm and I have a little Fiddle.php file for testing things.

I put in code like this:

class MyClass extends  {
}

When I change it to:

class MyClass extends MODX\Revolution\ {
}

When I type the final \, PhpStorm shows me a list of the classes that could come next. The continues for subsequent backslashes.

It’s a little funky sometimes because of the complexity of the project and MODX’s aliases, but it usually works. At times, it’s the only way I’ve been able to come up with a working fully qualified class name like the one for this processor:

MODX\Revolution\Processors\System\Dashboard\Widget\Remove

2 Likes