Create database table on install of MODx Extra

How can I have my transport package create a database table on install? I’ve reviewed the Rev Docs, Bob’s Guides, various other MODx contributor’s blogs and have come up short every test install I’ve done.

Here’s what I’ve in my resolver:

if ($object->xpdo) {
    switch ($options[xPDOTransport::PACKAGE_ACTION]) {
        case xPDOTransport::ACTION_INSTALL:
        case xPDOTransport::ACTION_UPGRADE:
            $modx =& $object->xpdo;

            $modelPath = $modx->getOption('package.core_path',null,$modx->getOption('core_path').'components/package/').'model/';
            if (!file_exists($modelPath)) break;

            if (!$modx->addPackage('package', $modelPath)) {
                $modx->log(modX::LOG_LEVEL_ERROR, "[package] was unable to load this package!");
                return false;
            }

            $modx->log(modX::LOG_LEVEL_INFO, ' ');
            $modx->log(modX::LOG_LEVEL_INFO, '[package] Creating the database table...');

            $manager = $modx->getManager();
            if(!$manager->createObjectContainer('Table')) {
              $modx->log(modX::LOG_LEVEL_INFO, ' ');
              $modx->log(modX::LOG_LEVEL_ERROR, '[package] Failed to create the database table...');
              $modx->log(modX::LOG_LEVEL_INFO, ' ');
              $modx->log(modX::LOG_LEVEL_ERROR, 'Please manually create a database table for package! ');
              $modx->log(modX::LOG_LEVEL_ERROR, 'The name should be something like: "modx_Table"');
              $modx->log(modX::LOG_LEVEL_ERROR, 'Where "modx_" is the prefix of your modx database install!');
            } else {
              $modx->log(modX::LOG_LEVEL_INFO, ' ');
              $modx->log(modX::LOG_LEVEL_INFO, '[package] Successfully created the database table...');
            };

            break;
        case xPDOTransport::ACTION_UNINSTALL:
            $modx =& $object->xpdo;

            $modelPath = $modx->getOption('package.core_path',null,$modx->getOption('core_path').'components/package/').'model/';
            if (!file_exists($modelPath)) break;

            if (!$modx->addPackage('package', $modelPath)) {
                $modx->log(modX::LOG_LEVEL_ERROR, "[package] was unable to load this package!");
                return false;
            }

            $manager = $modx->getManager();
            if(!$manager->removeObjectContainer('Table')) {
              $modx->log(modX::LOG_LEVEL_INFO, ' ');
              $modx->log(modX::LOG_LEVEL_ERROR, '[package] Failed to delete the database table...');
              $modx->log(modX::LOG_LEVEL_INFO, ' ');
              $modx->log(modX::LOG_LEVEL_ERROR, 'Please manually delete the database table for package! ');
              $modx->log(modX::LOG_LEVEL_ERROR, 'The name should be something like: "modx_Table"');
              $modx->log(modX::LOG_LEVEL_ERROR, 'Where "modx_" is the prefix of your modx database install!');
            } else {
              $modx->log(modX::LOG_LEVEL_INFO, ' ');
              $modx->log(modX::LOG_LEVEL_INFO, '[package] Successfully deleted the database table...');
            };
            break;
    }
}
return true;

My package talks to the database just fine while being used and the table has been manually created. Just won’t create it during install. I’m using MyComponent to create the package if that helps…

Well createObjectContainer should create the database table and you’ve got that there.
Table is the class name, right?
Are you getting any of the logs from the resolver showing in the installation log?
Any errors in your MODX log?

Is the resolver running at all? (sorry I haven’t used MyComponent before.)

You know, good point. I don’t even think the resolver is even running. How would I check and/or force it to execute?

Normally you’d reference the resolver in the package’s install script.
You might need to read up on how MyComponent does that. If @bobray is around he could probably offer his insights too.

So I created my own build script and everything is working correctly!

Thanks @digitalpenguin for your help even though it seemed superfluous… I appreciate it.

Regards! =)

1 Like