How to make a MODX extra using MyComponent Part 2

Share this on:

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:

  1. Write the XML schema for you table
  2. Run build.schema.php from your browser to build the xPDO class and map files
  3. Run .....$m->createObjectContainer('Doodle'); in a snippet to build the table.
One of the things that confused me was what to call my classes and tables. In the 'Doodles' tutorial the base class is called Doodles and the xml schema for the custom table has the package="doodles", table="doodles" and the class="Doodle" . I thought maybe there was a convention to have your base class with the plural, capitalized name of your table. There isn't and you don't.

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!

Share this on:
Mike Nuttall

Author: Mike Nuttall

Mike has been web designing, programming and building web applications in Leeds for many years. He founded Onsitenow in 2009 and has been helping clients turn business ideas into on-line reality ever since. Mike can be followed on Twitter and has a profile on Google+.

2 comments
  1. Nick

    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.

  2. Mike Nuttall

    Mike Nuttall
    Jul 05, 2015 at 11:48 AM

    Hi Nick,

    Glad you found it useful :-)

    Mike

Add a Comment



notify me of new replies


Allowed tags: <b><i><br>