We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45613
    • 45 Posts
    To create my site, I am going to need to create some custom tables, CMPs and other things.
    I have been reading Bob Ray's Guides on Custom Database tables - http://bobsguides.com/custom-db-tables.html and the Doodles tutorial.

    I am looking for some suggestions or best practices for ease in development and ongoing support.

    Some of the things I am thinking about are
    which approach is better or pro/cons for

    upgrading modx code without losing all the custom code

    If I make the custom code a package, I can test and upgrade without having to install both modx core and custom code?

    If I break up the custom code into small packages, I can maintain and test them separately and install them as needed?

    By making several smaller packages does it make it harder to link them together and add potential performance issues compared to more tightly coupled classes?

    Any links to other planning or development info / tutorials would be appreciated too.

    I am excited to get started, but I hope a little upfront planning can save me a bunch of time and rework later.
    Thanks in advance for your thoughts and feedback.
    bill
      • 3749
      • 24,544 Posts
      Either approach would work fine and neither should be affected by upgrading MODX. You should never, ever, modify the actual MODX core code (what's in the core and manager directories). It's relatively easy to create your own classes that extend the MODX classes if you need to add fields.

      Generally your code, when installed, will go in core/components/componentName/ or assets/components/componentname/, so it will be completely independent from the MODX code.

      Putting the code in a single or multiple packages shouldn't make any real difference unless the packages conflict with each other. There may some advantage to putting them all in one package, though, since every package gets its own namespace. If they all share a single namespace you won't have to remember which namespace goes with which package and you can also share any lexicon files between them. See the Login package for an example of multiple functions packaged together: https://github.com/splittingred/Login

      The method in my tutorial is geared to creating xPDO classes where you have one or more unrelated tables. It just saves you from having to create a schema and provides a convenient way to generate both the schema and the class and map files. If you'll have related tables and intersect objects, you'd have to rewrite the schema to include them (though I think I'd probably still do it my way and just add the related objects and aliases to the schema).

      All that said, If I were in your place, I'd use MyComponent to create the packages. It's a little difficult at first, but once you get the basics and the workflow down, you can create a new package in very little time. I can create a completely new package in about 20-30 minutes at the most (not counting writing the code, which you have to do with any method).

      Let me also recommend PhpStorm if you don't have it already. It's very easy to make an entire MODX install into a project and do your own work within it. MyComponent puts all your work in assets/mycomponents/componentname/. PhpStorm will know about all of your classes and all of the MODX and xPDO classes (and all their methods) and will autocomplete any of them and give you argument hints. It will save you a ton of time and will pay for itself in less than a week. As an example, I can type $modx->makeUrl(), put the cursor on it and type Ctrl-b and PhpStorm will open the the MODX file containing that method and put the cursor on it in about a second -- Ctrl-Alt-left-arrow will take me right back to my own code.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 45613
        • 45 Posts
        Thanks Bob for the detailed response. I have not used phpStorm but it looks like it may be worth taking a look at it.

        Would like to get your thoughts on your deployment best practices.

        If you just fix some bugs in a few components, would you just replace those files?
        if you just need to upgrade the modx core just replace that directory?
        or would you zip/tar up everything and replace everything?

        Thanks,
        bill
          • 3749
          • 24,544 Posts
          bill, I'm not sure I understand your question, but maybe this will help.

          For my own components, since every package has a version. Even to update a file or two, I up the version number and transfer the whole package. I have over 30 add-on packages, and if I didn't do that, I'd never know what state a given package was in on a given site.

          For the same reason, when upgrading MODX in any way, I always download the new version and run setup. http://bobsguides.com/modx-upgrade-faq.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 45613
            • 45 Posts
            Thanks again Bob.
            Sorry the question was worded badly, but your answer got me what I needed.

            Thanks,
            bill
              • 45613
              • 45 Posts
              So how do you create the xPDO classes and schema files?

              Use your tables steps, or something like CMPgenerator?

              Would I put these in /assets/mycomponents/example/core/components/example/model/example - (replacing example with myprojectName)?

              Then build step in mycomponent should pick all these up. Do I need to create some sql to build the table it if does not exist, or does the install process take care of this?

                • 3749
                • 24,544 Posts
                Quote from: bs28723 at Nov 30, 2013, 08:44 PM
                So how do you create the xPDO classes and schema files?

                Use your tables steps, or something like CMPgenerator?

                I use my tables or create a new schema by modifying an existing one


                Would I put these in /assets/mycomponents/example/core/components/example/model/example - (replacing example with myprojectName)?

                That's the right path, but with MyComponent, you edit the new project config file and MC puts them all where they belong (unless they exist already).


                Then build step in mycomponent should pick all these up. Do I need to create some sql to build the table it if does not exist, or does the install process take care of this?

                In theory, yes, though you first need to run Bootstrap and ExportObject in MyComponent after editing the project config file. You don't need any SQL to create the tables, but you do need some PHP in a resolver. It's as simple as this:


                $package = 'Example';
                $modelPath=MODX_CORE_PATH . '/components/example';
                $prefix = 'myPrefix_';
                $tableName = 'MyTable';
                
                $modx->addPackage($package, $modelPath, $prefix);
                $manager = $modx->getManager();
                $manager->createObjectContainer($tableName);
                



                [ed. note: BobRay last edited this post 12 years, 10 months ago.]
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  $manager->createObjectContainer(tableName);

                  Should that be
                  $manager->createObjectContainer($tableName);

                  or does some arcane xPDO thing deal with it without the $?
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 45613
                    • 45 Posts
                    Great Thanks. One more question on this thread...

                    There is an SDK download that includes "MODX Revolution build assets, ExtJS debug files, and additional features for MODX Extras developers".

                    In your experience for developing extras, and I know I will need a few CMPs, should I use this for my local development environment?

                    Thanks again for all the help, I know this is going to same me a lot of time.
                      • 3749
                      • 24,544 Posts
                      Quote from: sottwell at Dec 01, 2013, 01:49 AM
                      Should that be 
                      $manager->createObjectContainer($tableName);
                      or does some arcane xPDO thing deal with it without the $?[/quote] Yup. It was $this->tableName in the code I pasted and I over-shortened it. ;)
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting