How to make a MODX extra using MyComponent Part 2
Posted by Mike Nuttall
Getting to grips with MyComponent
In the first article I described the first steps to developing an extra in MODX. It's easy to go wrong and patience and persistence are needed. I found the best approach was to follow the tutorials closely and make notes as I learned.
At first with MyComponent I couldn't get anything to work. I had to slow down a bit and be more systematic. I found the best approach was to make small changes build the transport package and check that my changes carried across to a new install. The problems I encountered and the solutions I found are described in the previous article.
In this article I take the next step and develop a custom table for my extra.
Building a custom table
I followed the same procedure as described in the first part of Developing an Extra in MODX Revolution Part 1 which is a 3 part process:
- Write the XML schema for you table
- Run build.schema.php from your browser to build the xPDO class and map files
- Run .....$m->createObjectContainer('Doodle'); in a snippet to build the table.
When you create a new package with MyComponent, say myProject
MyComponent will create your base class for you in model/myproject/myproject.class.php
You can call your custom table whatever you want, say myItems ( In your schema file you will have package="myitems", table="myitems" and class="Myitem". Object singular, records plural)
When you run build.schema.php from your browser it will create model/myitem/myitem.class.php along with the other class and map files.
The reason this can be confusing in the Doodles tutorial is that the base class doodles.class.php and the class file for the custom table doodle.class.php are in the same folder. You don't have to do it that way, and I chose not to at first while learning MyComponent, to save confusion.
The important thing to remember is that you get access to you table by using:
$this->modx->addPackage('myitems',$this->config['modelPath']);
in your base class, or
$modx->addPackage('myitems',$this->config['modelPath']);
in a snippet.
Adding a resolver for a custom table using MyComponent
So having created a new project in MyComponent then followed the first part of the 'Doodles' tutorial to build my custom table I wanted to see if I could create the table on a new install using the transport package.
This turned out to be a lot easier than I thought. MyComponent helpfully provides a stub resolver for you in _build/resolvers/myproject.resolver.php
if ($object->xpdo) { $modx =& $object->xpdo; switch ($options[xPDOTransport::PACKAGE_ACTION]) { case xPDOTransport::ACTION_INSTALL: /* */ break; case xPDOTransport::ACTION_UPGRADE: /* */ break; case xPDOTransport::ACTION_UNINSTALL: break; } } return true;
You can then just add the appropriate code (As demonstrated in Developing an Extra in MODX Revolution, Part III )
if ($object->xpdo) { $modx =& $object->xpdo; switch ($options[xPDOTransport::PACKAGE_ACTION]) { case xPDOTransport::ACTION_INSTALL: $modx =& $object->xpdo; $modelPath = $modx->getOption('core_path').'components/myproject/'.'model/'; $modx->addPackage('myitems',$modelPath); $manager = $modx->getManager(); $manager->createObjectContainer('Myitem'); break; case xPDOTransport::ACTION_UPGRADE: /* */ break;<p> case xPDOTransport::ACTION_UNINSTALL: break; } } return true;
This worked great!
So now I am ready to do some developing using MyComponet with custom tables and custom manager pages. And with the knowledge that I'll be able to package it up and share it between my various installs and the wider community. Fantastic!
Nick
Jul 05, 2015 at 04:26 AM
Hey Mike,
I know this post is rather old, but wanted to say thanks as this was extremely helpful in getting mycomponent working with custom tables.
Mike Nuttall
Jul 05, 2015 at 11:48 AM
Hi Nick,
Glad you found it useful :-)
Mike