We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48391
    • 6 Posts
    Hi all,
    I'm trying to create a modx component and i need to associate a few tvs with a template. What i currently have is:

    $template = $modx->newObject('modTemplate');
    $template->fromArray(
        array(
            'id' => 1,
            'templatename' => 'template_name',
            'description' => '',
            'editor_type' => '',
            'icon' => '',
            'locked' => '0',
            'content' => '',
            'properties' => '',
        ),
        '',
        true,
        true
    );
    
    
    $tv1 = $modx->newObject('modTemplateVar');
    $tv1->fromArray(
        array(
            'id' => 1,
            'type' => 'checkbox',
            'name' => 'tv_name',
            'caption' => '',
            'description' => '',
            'display' => 'default',
            'elements' => 'Monday==monday||Tuesday==tuesday||Wednesday==wednesday||Thursday==thursday||Friday==friday||Saturday==saturday||Sunday==sunday',
            'locked' => 0,
            'rank' => 4,
            'default_text' => '',
            'input_properties' => array('allowBlank' => 'false',),
            'properties' => array(),
        ),
        '',
        true,
        true
    );
    
    
    
    $rel = $modx->newObject('modTemplateVarTemplate');
    $rel->addOne($tv1, 'TemplateVar');
    
    
    $rels = array();
    $rels[] = $rel;
    $template->addMany($rels, 'TemplateVarTemplates');


    This code is doing what it has to do but the problem arise when i try to install the component. I get the following error:

    Error 42S22 executing statement: Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'modTemplateVar.templatename' in 'where clause' )


    Anyway everything is working but i need to understand what is causing this error. Would be great if someone can give me a point !!!

    Thanks in advance
      • 3749
      • 24,544 Posts
      This is wrong:

      'templatename' => 'template_name',


      It should be:

      'name' => 'template_name',


      BTW, the MyComponent extra will do this all for you and quite a bit more. It might not be worth taking the time to learn it if you only have one extra and/or it's very simple. If you're going to do more, though, it would be well worth it.

      I can usually create a new package with MC in about 20 minutes (not counting writing the actual 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
        • 48391
        • 6 Posts
        Bob,
        I tried your suggestion but it seems it's not correct because when i change templatename to name the created template doesn't have a name at all.
        The component i'm working on is a bit complicated ... i already tried PackMan but it's not able to do the whole job. I'll give a try to MyComponent as well(thanks for this). I know i can achieve it by using resolver but i want to do it the right way so more i know about modx ... better. Feel free to add more suggestions if you have ....
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Yeah, the field in the site_templates table is 'templatename'
            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
            • 40358
            • 40 Posts
            Looking at modtemplate.map.inc.php, it looks like modTemplate does not have a composite towards modTemplateVar.
              • 3749
              • 24,544 Posts
              The reason MyComponent always does it with a resolver is that Package Manager sometimes has trouble with nested objects, so if you add the object to a category in build.transport.php, objects added with addOne() or addMany() prior to that don't always come along for the ride.

              All the resolvers are automatically updated when you run ExportObjects, so if the objects in MODX are connected, they will be in the install too.
              [ed. note: BobRay last edited this post 12 years, 2 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
                • 48391
                • 6 Posts
                @optimus : interesting point ... i will definitely take a look at it. thanks for a millions

                @bob : i installed MyComponent yesterday and it looks very promising. initially i was a little confused but playing with it for a few hours made more sense. i think it is able to do the job. great work !
                  • 3749
                  • 24,544 Posts
                  Thank you. I hope you find it useful. smiley

                  It will take you quite a while to edit your first project config file correctly, but once you get used to it, you can just delete the stuff you don't need and fill in the stuff you need pretty quickly.

                  It kicks butt for handling lexicon strings and creating complex objects like TVs and property sets (you just create them in the Manager and run ExportObjects).
                    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
                    • 48391
                    • 6 Posts
                    Bob,
                    I invested some time playing with MyComponent and some questions arised. Does it support creating form customisations?
                    I have 2 namespaces defined in my project config and when i bootstrap the component all defined namespaces are properly imported into the manager but building a package and installing it creates just the default namespace. Do i have to manually edit build.transport.php so all the namespaces to be created properly? I found that when i go and edit my stuff in the manager, export the component after that, remove objects, bootstrap again some of my tvs are defined to be of type RichText but actually they were imported as a Text. Do you have an idea what can cause such a behavior?

                    - consider the following scenario: bootstrap the component, change the rank of some of the template variables in the manager, exportobject to get the changes, removeobjects, bootstrap again. everything works as expected but the rank is not preserved
                    - consider the following scenario: create a template in the manager, create a project config and list the same template to be part of your project, bootstrap, removeobjects. what i expect is to see the previously created template but it is deleted by removeobjects command

                    i think it will be more useful if you handle the following commands like this:
                    Bootstrap - automatically create transport package and install it through modx api
                    RemoveObjects - uninstall the package [ed. note: ppaskov last edited this post 12 years, 2 months ago.]
                      • 3749
                      • 24,544 Posts
                      On Form Customization: yes and no. It doesn't create the rules automatically, but you can add a resolver to create them (or add the creation code to an existing resolver). It probably will do them automatically someday.

                      Namespaces: The package itself can have only one Namespace (a limitation of Package Manager). It might work to specify others (below the package namespace) in the 'namespaces' member of the project config file. If not, you could create them in a resolver as above (or maybe better -- in a validator, so they'll be there before installation). I try not to modify the build.transport.php file unless absolutely necessary. You can *almost* always accomplish what you want in a resolver. Once you list a custom resolver in the project config file and create it, it won't be touched by MyComponent. Just be sure it has a unique name or is named for the package.

                      RemoveObjects nukes all the objects in MODX. If you run Bootstrap after that, they'll be recreated as described in the config file, then exported in that form by ExportObjects. That's why you're losing the rank. If you specify the 'rank' field in the project config file where you describe the TVs, it should be preserved, but I'm not 100% positive about that.

                      Personally, I never run RemoveObjects unless something has gone seriously wrong and I want to start over (IOW, almost never).

                      It's a failing of MC that when you update an object in MODX and run ExportObjects, the project config file is not updated. I mean to do it someday, but it's immensely complicated. That's why it's a good idea to update the project config file often and *not* a good idea to ever run RemoveObjects.

                      i think it will be more useful if you handle the following commands like this:
                      Bootstrap - automatically create transport package and install it through modx api
                      RemoveObjects - uninstall the package

                      Bootstrap is designed to be non-destructive so that you can add objects to the project config file and install them in MODX with Bootstrap without affecting existing objects. Doing it the way you suggest would overwrite objects in MODX based on the config file (which, much of the time, will not contain all the information you need for those objects - e.g., properties and TV options). You'd risk losing valuable work.

                      RemoveObjects is designed to nuke all the objects in the case where you've messed up and want to start all over, and uninstalling the package in Package Manager doesn't work correctly. Actually, I find it works best not to ever install the package on the dev. site using Package Manager. When I'm done (or nearly done) with a package, I build it and install it on another site. Using one site for developing the package and another for testing the install very often turns up problems with the install that won't show up on the dev. site because there are components or relationships there that aren't created in the actual install but need to be.



                        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