We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32366
    • 6 Posts
    Hi,

    Am trying to create an Extra, I’ve followed the doodle tutorial http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution

    It seems to work, except that when I run the snippet - the DB table is not created... I’ve been on this a while, but am having a tough time.

    I have tried adding the setting: xPDO::OPT_AUTO_CREATE_TABLES, but am not sure if the syntax was correct:

    $dood = $modx->getService('doodles','Doodles',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/doodles/').'model/doodles/',$scriptProperties);
    if (!($dood instanceof Doodles)) return '';
     
    /* setup default properties */
    $tpl = $modx->getOption('tpl',$scriptProperties,'rowTpl');
    $sort = $modx->getOption('sort',$scriptProperties,'name');
    $dir = $modx->getOption('dir',$scriptProperties,'ASC');
    
    $modx->setOption('xPDO::OPT_AUTO_CREATE_TABLES',true);
     
    $m = $modx->getManager();
    $m->createObjectContainer('Doodle');
    return 'Table created.';
    
    


    As you can see I’ve tried setting xPDO::OPT_AUTO_CREATE_TABLES to true, but the syntax may be wrong - or that is not the problem.
    It returns ’Table created’, but the table isn’t created. I can’t view my mysql log file, though the apache and php logs are both fine.

    Any idea will be more idea than I have at the moment.

    Thanking you
      • 22303 MODX Staff
      • 10,725 Posts
      First, you don’t need the xPDO::OPT_AUTO_CREATE_TABLES option if you are calling createObjectContainer() explicitly.

      Second, that is a class constant, not a string, so it should be:
      <?php
      $modx->setOption(xPDO::OPT_AUTO_CREATE_TABLES, true);
        • 14883 ☆ A M B ☆
        • 450 Posts
        I’m trying to glean just enough from this tutorial to use createObjectContainer to create tables from my custom class files.
        <?php
          $invoice= $modx->getService('invoice','Invoice',MODX_CORE_PATH.'packages/mscd/model/mscd/',$scriptProperties);
        if (!($invoice instanceof Invoice)) 
        {return 'fail';}
          
          $m = $modx->getManager();
          
        $results = $m->createObjectContainer('Invoice');
          $msg = ($results) ? 'Invoice Table created.' : 'Invoice Table failed.';
          return $msg;​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


        I’m getting:
        Fatal error: Class 'Invoice_mysql' not found in /home/my/path/core/xpdo/xpdo.class.php on line 550


        I do have the correct class directory structure, including the mysql subdirectory that defines class Invoice_mysql extends Invoice {}​.

        Any idea what I’m doing wrong?

        Also, a few follow-up questions:

        • does this table get created in the MODx database?
        • is there any way to specify a prefix?

          • 22303 MODX Staff
          • 10,725 Posts
          First, you definitely don’t want to point to the core/packages/ directory for your model, i.e. MODX_CORE_PATH.’packages/mscd/model/mscd/’ is not likely what you want. Second of all, why are you using getService to try and load a table class? This is for service classes, which are not generated classes in your model; these are typically a main class which provides business logic and/or common methods for your custom model. All you need to do to be able to create your table is to call $modx->addPackage() and you should be good to go.
            • 14883 ☆ A M B ☆
            • 450 Posts
            Quote from: OpenGeek at Jun 29, 2011, 01:30 PM

            First, you definitely don’t want to point to the core/packages/ directory for your model, i.e. MODX_CORE_PATH.’packages/mscd/model/mscd/’ is not likely what you want.

            Noted. By-product of getting it wrong the first time I ever created classes, and using the same snippet ever since. I have changed it to MODX_CORE_PATH.’components/mscd/model/mscd/’. Is that better/correct?

            Quote from: OpenGeek at Jun 29, 2011, 01:30 PM

            Second of all, why are you using getService to try and load a table class? This is for service classes, which are not generated classes in your model; these are typically a main class which provides business logic and/or common methods for your custom model. All you need to do to be able to create your table is to call $modx->addPackage() and you should be good to go.

            Because I am so clueless as to what some of these methods are properly used for; I’m fumbling in the dark. I did a search for createObjectContainer and found this forum thread, which referenced the Doodles tutorial, which got at the classes via getService.

            So now my code looks like this:
            <?php
            $modx->addPackage('mscd',MODX_CORE_PATH.'components/mscd/model/mscd/',''); 
            $m = $modx->getManager();
            $results = $m->createObjectContainer('Invoice');
            $msg = ($results) ? 'Invoice Table created.' : 'Invoice Table failed.';
            return $msg;​


            The page returns "Invoice Table failed", and the error message shows this:
            (ERROR @ /index.php) Could not load class: Invoice from mysql.invoice.


            Is this progress?

            Related question I forgot to ask before: the return statement in the code block from the 1st post came from the Doodles tutorial, and I think is poorly written (see the author’s result). I think it should be replaced in the tutorial with one like I have above, based on the actual results of createObjectContainer. Do you agree? If so, I can update the tutorial accordingly.
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: jrotering at Jun 29, 2011, 03:28 PM

              I have changed it to MODX_CORE_PATH.’components/mscd/model/mscd/’. Is that better/correct?
              No, it should be pointing to the root location of your model folder, not to the actual folder with the package name, i.e.
              MODX_CORE_PATH.'components/mscd/model/';


              Quote from: jrotering at Jun 29, 2011, 03:28 PM

              Related question I forgot to ask before: the return statement in the code block from the 1st post came from the Doodles tutorial, and I think is poorly written (see the author’s result). I think it should be replaced in the tutorial with one like I have above, based on the actual results of createObjectContainer. Do you agree? If so, I can update the tutorial accordingly.
              No, the Doodles tutorial is describing how to create an Extra that is loaded via a service class. This class serves as a central class to the custom Doodles model, but is not representing a custom table itself. It is responsible for adding the package and other things that some Extras might want to do. Getting the xPDOManager instance and calling it’s method to create the table could be done with or without this service class. In your case, you don’t have a service class, so you can simply add the package and create the table.
                • 14883 ☆ A M B ☆
                • 450 Posts
                Thanks, that finally worked once I changed the path.

                Regarding the Doodles tutorial: what I meant was, this block of code:
                $m = $modx->getManager();
                $m->createObjectContainer('Doodle');
                return 'Table created.';


                Is always going to return "Table created.", even when something fails and no table is created. I know that I shouldn’t have been looking in that tutorial at all for my purposes, but for its intended purpose, wouldn’t that block make more sense as something like:
                $m = $modx->getManager();
                return ($m->createObjectContainer('Doodle')) ? 'Table created.' : 'Table not created';
                


                Just trying to make the tutorial more clear.
                  • 28215
                  • 4,149 Posts
                  Good catch. Adjusted.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 42838
                    • 28 Posts
                    I have the same problem. parseSchema success created classes from my scheme model. so i can add package and i can use newQuery make query (i look my query use method prepare and toSQL), but i don't create table in my base. Where can I see the error.
                      • 3749
                      • 24,544 Posts
                      It would be really helpful, imo, to have include/require (class file), getService(), loadClass(), and addPackage() compared on the same page in the docs with a simple but full description of what they do, and when and why you might want to use one over the others. Maybe that exists, but I haven't seen 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