Migx cmp with modx 3

Hi,
i have trouble setting up a new cmp page with modx 3. It always worked with modx 2 and an existing cmp does also work with modx 3. i just can’t create a new one.

i can create my schema, and i can also parse it. but when i want to create the tables i get a 500 error in the developer console on this url: https://www.rifflblech.com/assets/components/migx/connector.php. no additional details and also no output in modx’s error logs.

my xml schema:

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

<model package="events" baseClass="xPDOObject" platform="mysql" defaultEngine="InnoDB" version="1.1">

    <object class="Event" table="events" extends="xPDOSimpleObject">
        <field key="name" dbtype="varchar" phptype="string" precision="250" null="false" default="" />
        <field key="play_time" dbtype="varchar" phptype="string" precision="250" null="false" default="" />
        <field key="play_place" dbtype="varchar" phptype="string" precision="250" null="false" default="" />
        <field key="play_mode" dbtype="int" precision="10" phptype="integer" null="false" default="0"/>
    </object>
</model>

I follow this tutorial: MIGX DB CMP and XPDO in MODX Revolution - YouTube

Are there any known issues? Is there a newer tutorial?

Thanks

With MODX 3 there have been some changes. For example the schema is slightly different.

Take a look at this article that is specifically for MODX 3

https://docs.modx.com/3.x/en/extending-modx/tutorials/using-custom-database-tables

or maybe watch this video.


Specifically for MIGX you need the newest version 3.0.0-beta1.
Take a look a this thread:

try to use 3.0.0-beta1 from here
MIGX/migx-3.0.0-beta1.transport.zip at master · Bruno17/MIGX (github.com)

this will create an example schema and a file structure, the MODX 3 way and you should be able to create and manipulate your tables from schema

How to install?

I downloaded the migx-3.0.0-beta1.transport.zip file and what next?

for installing Extras manually, see here:

Package Management - Building Sites | MODX Documentation

In MODX 3 you can also just use drag&drop. Open the Package Management (Extras → Installer) and drag the zip-file to the grid.

Thanks, drag&drop is great!

Unfortunately, I still have an error.

So far I have done:
1.I installed a version of
https://github.com/Bruno17/MIGX/blob/cb66973d03f03e67eb51ab80eaaef661aad00e9b/_packages/migx-3.0.0-beta1.transport.zip
2. I implemented an example:
https://docs.modx.com/3.x/en/extras/migxdb/migxdb.tutorials/create-doodles-manager-with-help-of-migxdb

Problem is in Create Table. When I click the button: “Create Tables” nothing happens. But when I check the console in Chrome there is an error:
/assets/components/migx/connector.php:1 Failed to load resource: the server responded with a status of 500 ()

How to fix it?

In general, I need to make a module to edit any table from the database in the CMS.
Maybe someone has another example of how to do this?

The schema in this example is out of date. (This is for MODX 2.)

When you create a new package, MIGX creates a sample schema in the package directory.
Take a look at that schema and adjust it to your needs.

Is there any way to just point to a table from the database and it will automatically create a whole module to edit it?

What exactly are you trying to do?

If you want to be able to change the data of one of the existing core MODX database table in MIGX, you can set the “Classname” in the MIGX configuration (tab “MIGXdb-Settings”) to (for example) MODX\Revolution\modResource.

I want to be able to do editing of some custom tables that I added to the database.

For example:
I want to make a dictionary for basic words found on a website. I have added a table with 4 fields (id, en, fr, de) to the database and need to be able to edit this table.

Sample record:
id: 1
en: more
fr: plus
de: mehr

image

CREATE TABLE `dictionary` (
  `id` int(11) NOT NULL,
  `en` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `fr` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `de` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


INSERT INTO `dictionary` (`id`, `en`, `fr`, `de`) VALUES
(1, 'more', 'plus', 'mehr');

It’s usually better to start with the schema, than to create the custom table manually.

In MIGX there is a way to create a schema from existing database tables, by using the “Write schema” button in the “Package Manager”. But you probably need a custom table prefix for it to work properly (and there is still a bug in MIGX for custom table prefixes in MODX 3).

Also, the table you created misses a proper (autoincrement) primary key.


If I was you, I would create a new package in MIGX with the name “Mycompany”.
Then use a schema like this:

<?xml version="1.0" encoding="UTF-8"?>
<model package="Mycompany\Model\" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" version="3.0">
    <object class="Dictionary" table="mycompany_dictionary" extends="xPDO\Om\xPDOSimpleObject">
        <field key="en" dbtype="varchar" phptype="string" precision="100" null="false" default="" />
        <field key="fr" dbtype="varchar" phptype="string" precision="100" null="false" default="" />
        <field key="de" dbtype="varchar" phptype="string" precision="100" null="false" default="" />
    </object>
</model>

And in the MIGX configuration → tab “MIGXdb-Settings” → set the field “Classname” to Mycompany\Model\Dictionary.

1 Like

Here is an example if you want to create the database table first:

  1. Create the table in SQL with a custom prefix (here myprefix_)
CREATE TABLE `myprefix_dictionary` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `en` varchar(100) NOT NULL DEFAULT '',
  `fr` varchar(100) NOT NULL DEFAULT '',
  `de` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARSET=utf8mb4;
  1. In the MIGX “Package Manager” set “Package Name:” to Mypackage, “table-prefix:” to Custom Prefix and “custom-prefix:” to myprefix_. Click the button “Create Package”.
  2. Click the button “Write schema” (tab “Schema”).
  3. Check if the schema was created correctly. Go to the tab “Xml Schema” and click the button “Load schema” (or open the file core/components/mypackage/schema/mypackage.mysql.schema.xml). The schema should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<model package="Mypackage\Model\" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" version="3.0">
	<object class="Dictionary" table="dictionary" extends="xPDO\Om\xPDOSimpleObject">
		<field key="en" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
		<field key="fr" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
		<field key="de" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
	</object>
</model>
  1. Click the button “parse Schema” (tab “Schema”).
  2. Change the bootstrap file: Open the file core/components/mypackage/bootstrap.php and make sure the addPackage function looks like this (with the custom prefix as the third parameter).
$modx->addPackage('Mypackage\\Model\\', $namespace['path'] . 'src/', 'myprefix_', 'Mypackage\\');
  1. Create a MIGX configuration:
  • Tab “MIGXdb-Settings”
    • Classname = Mypackage\Model\Dictionary
  • Tab “Actionbuttons”
    • addItem
  • Tab “Contextmenues”
    • update
    • remove
  • Tab “Columns”
    • add columns for “id”, “en”, “fr” and “de”
  • Tab “Formtabs”
    • add fields for “en”, “fr” and “de”
  • Tab “Settings”
    • choose a unique name (e.g. mydictionary) for “Name” and “unique MIGX ID”
  1. Create a new menu item:
  • “Action:” = index
  • “Namespace:” = migx
  • “Parameters:” = &configs=mydictionary (the “Name” from the MIGX configuration)
2 Likes

Thank you very much for such a thorough explanation. The editing works, great!

I still have a small question, how to set the title of the tab:
image

I set MIGX > Settings:
(a) Form Caption: Dictionary
(b) Window Title: Dictionary

But I still have “undefined”

These settings are in the tab “CMP-Settings” (in the MIGX configuration).

1 Like

That was the point, thank you very much again! :handshake:

I am writing a simple snippet to extract data from this table but I keep getting an error. I wanted to fetch, for example, the record “id=1” and output the column “en”

$modx->addPackage('Mypackage\Model',MODX_CORE_PATH.'components/Mypackage/src/Model/');
$dictionary = $modx->getObject('Dictionary', 1);
$en = $dictionary->get('en');

Or, for example, I wanted to type “en=more” and retrieve the value from the “de” column

$modx->addPackage('Mypackage\Model',MODX_CORE_PATH.'components/Mypackage/src/Model/');
$dictionary = $modx->getObject('Dictionary', array('en' => 'more' ) );
$de = $dictionary->get('de');

You don’t need to use addPackage() in your snippet.
addPackage() is already executed in the file bootstrap.php when MODX is initialized.

This code should work

<?php
use Mypackage\Model\Dictionary;

$dictionary = $modx->getObject(Dictionary::class, 1);
$en = $dictionary->get('en');
return $en;

or alternatively this

<?php
$dictionary = $modx->getObject('Mypackage\\Model\\Dictionary', 1);
$en = $dictionary->get('en');
return $en;
1 Like

That was the point, thank you very much again!
MODX is so cool but I have trouble finding everything in the documentation.

One more problem. I can add new items, I can delete but when I want to edit it opens a window and all the fields are empty. I have selected “update” in “Contextmenues” is there still something extra to set?