Trying to generate some code for MODx with AI...Is it accurate?

This looks pretty good to me but I haven’t been under the hood since I submitted some pulls like 15 years ago. Sheesh.

The code is supposed to generate map, schema, and class file from a specified database table for adding new classes simply through data engineering.

Here’s the code:

// Set the class and table names
$class_name = 'MyClass';
$table_name = 'my_table';

// Include MODx config file for database config

// Connect to the database
$pdo = new PDO("mysql:host=$database_server;dbname=$dbase;charset=utf8mb4", $database_user, $database_password);

// Get the table columns
$columns = array();
$query = "SHOW COLUMNS FROM `$table_name`";
$stmt = $pdo->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $columns[] = $row['Field'];

// Generate the schema
$schema = array();
foreach ($columns as $column) {
    $schema[$column] = array(
        'type' => 'string', // You can set the data type based on the column's data type
        'dbtype' => 'text', // Set the database column type

// Generate the map
$map = array();
foreach ($columns as $column) {
    $map[$column] = array(
        'dbtype' => 'text', // Set the database column type
        'phptype' => 'string', // You can set the PHP data type based on the column's data type
        'null' => true, // Set whether the column can be null or not

// Generate the class
$class = <<<EOD
class $class_name extends xPDOSimpleObject {
    public function __construct(&\$xpdo) {

// Write the schema, map, and class files to disk
$schema_file = "core/components/mycomponent/model/schema/{$table_name}.schema.php";
$map_file = "core/components/mycomponent/model/{$class_name}";
$class_file = "core/components/mycomponent/model/{$class_name}.class.php";
file_put_contents($schema_file, "<?php\n\nreturn " . var_export($schema, true) . ";\n");
file_put_contents($map_file, "<?php\n\nreturn " . var_export($map, true) . ";\n");
file_put_contents($class_file, $class);

That is not the recommended way of working, nor does it look like it is even complete. Many things have changed in the 3.x branches of both xPDO and MODX. Specific to what it looks like you are trying to do, you can explore the new xPDO tool for generating schemas from existing database tables (write-schema) and parsing schemas to generate the model classes (parse-schema) in 3.x.

Once you have MODX 3.x installed, you can call the xPDO CLI tool directly from core/vendor/bin/xpdo…

❯ core/vendor/bin/xpdo help write-schema
❯ core/vendor/bin/xpdo help parse-schema

The core/model/schema/config.php is a good example of the config option you will need for those commands. You can see example usages for the MODX core in the composer.json file now…

        "parse-schema-mysql": [
            "core/vendor/bin/xpdo parse-schema --config=core/model/schema/config.php --update=1 --psr4=MODX\\\\ mysql core/model/schema/modx.mysql.schema.xml core/src/",
            "core/vendor/bin/xpdo parse-schema --config=core/model/schema/config.php --update=1 --psr4=MODX\\\\ mysql core/model/schema/modx.registry.db.mysql.schema.xml core/src/",
            "core/vendor/bin/xpdo parse-schema --config=core/model/schema/config.php --update=1 --psr4=MODX\\\\ mysql core/model/schema/modx.sources.mysql.schema.xml core/src/",
            "core/vendor/bin/xpdo parse-schema --config=core/model/schema/config.php --update=1 --psr4=MODX\\\\ mysql core/model/schema/modx.transport.mysql.schema.xml core/src/"

Thank you for the response and information @opengeek.

I was trying to stay away from CLI and use a form as a dashboard widget to generate new map/schema/class based on a specified table_name & ClassName so that all the files aren’t rewritten, just the one specified.

Years ago I wrote a dashboard widget called SMUT (Schema Map Update Tool) and if I wanted to generate a new class all I did was the data engineering and then type the table name for the new class into the SMUT tool and it would generate all this, however as you mentioned it’s broken now due top the 3.0.x updates.

I’d like to update that tool for compatibility with 3.0.x.

You don’t have to use the CLI.
I believe you can still use the function writeSchema to create the schema from the database table

and then use the function parseSchema to generate the class files from the schema.

There is some code in this Github issue that may or may not work.

Also maybe read this article from the documentation to see what has changed in MODX 3:

1 Like

Thank you @halftrainedharry.

I miss net neutrality. This new documentation didn’t come up in searches for me. In fact I get a lot of keyboard purchase links and tutorials for a keyboard when searching anything related to MODX.

Before NN ended I only got links to CMS MODX when doing searches. Coincidence? Likely not! Haha.