We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I finally took a serious whack at the procedure outlined in this article:

    http://svn.modxcms.com/docs/display/revolution/Using+Custom+Database+Tables+in+your+3rd+Party+Components

    But it left me seriously confused. Has anyone gotten this working?

    Specifically, I’d like to see that article simplified into component parts so it doesn’t make my head explode. I recommend the following topics (in this order)

    1. Once I’ve got an XML schema definition, how do I use it in my Snippets to access rows? E.g. MODx already has a bunch of XML files for accessing its own tables... what I’d love to see is how to use the xPDO API to get data from those tables.

    2. Making an xPDO XML schema definition file to match an existing table:
    a) show how to build the XML file manually, using existing files in /core/model/schema as starting points. I think this is handled pretty well here: http://svn.modxcms.com/docs/display/xPDO20/Defining+the+Database+and+Tables
    b) show how to use xPDO to automatically create the XML (i.e. reverse engineer the schema of an existing table). I’ve looked at the script posted here http://modxcms.com/forums/index.php?topic=40174.0 but I don’t have any idea what to do with the output. I see that the schema file WAS generated in /core/model/schema, but more explanation is probably needed. One note is that this script needs to be run as a web page (command line execution fails), e.g. http://domain.com/core/components/xyz/reverse_engineer.php More importantly, how do I use my schema file once I’ve got it? (see below)

    3. Using MODx and xPDO to create a new MySQL table based off a new schema definition.



    • Hello Everett,

      Did you took at the three How to’s on Revolution docs?
      http://svn.modxcms.com/docs/display/revolution/PHP+Coding+in+MODx+Revolution%2C+Pt.+I
      http://svn.modxcms.com/docs/display/revolution/PHP+Coding+in+MODx+Revolution%2C+Pt.+II
      http://svn.modxcms.com/docs/display/revolution/PHP+Coding+in+MODx+Revolution%2C+Pt.+III

      There are a lot of examples on how to interact with your database.
      • Thanks, those articles are good... but unfortunately they only cover the built-in objects (i.e. MODx internals). The bit I’m looking for is how to interact with my own custom objects... but that’s not covered. The promising quote:
        You can also define your own schemas for your own components and add them as packages - more on that in a future article.

        I want to get things off the ground interacting with my own custom tables. If I could see a couple examples, I could get this going...

        Thanks!
          • 28215
          • 4,149 Posts
          Quote from: Everett at Nov 02, 2009, 02:01 AM

          Thanks, those articles are good... but unfortunately they only cover the built-in objects (i.e. MODx internals). The bit I’m looking for is how to interact with my own custom objects... but that’s not covered. The promising quote:
          You can also define your own schemas for your own components and add them as packages - more on that in a future article.

          I want to get things off the ground interacting with my own custom tables. If I could see a couple examples, I could get this going...

          Thanks!

          Everett,

          So, I’m assuming you’ve built your model maps and classes, you’ll just run this function:

          $modx->addPackage('mypackagename','/path/to/my/model/');
          
          // ie, for a package "test" in your "core/components/myapp/model/test/'
          $modx->addPackage('test',$modx->getOption('core_path').'/components/myapp/model/');
          


          This will add all the maps to the current xPDO instance so that it can grab the object definitions. You can then grab your classes (table objects) by doing:

          $modx->getObject('test.myObject');
          
          // or, alternatively, but a tiny bit slower, because it has to search for the package:
          $modx->getObject('myObject');
          


          Feel free to ask further questions!
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 3749
            • 24,544 Posts
            @splittingred: Now you’ve confused me. tongue

            How does loadClass() fit into this picture (assuming that it does)?

            I would have thought you’d have to load a class before you could uses getObject(), new, or newObject() on it.

            It think it would help if I knew what or where the class and package are being loaded or added *to*.

            Part of my confusion stems from the fact that captcha.php has this code in it (I think you know how it got there wink ):

            $modx->addPackage('captcha',$captcha_core_path.'model/');
            $modx->loadClass('captcha.VeriWord',$captcha_core_path.'model/',true,true);


            Is one or the other of these lines not needed or do they do different things?

            (I hope this isn’t hijacking the thread -- I’m confused enough that I can’t tell).
              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
            • Hello BobRay,

              Here is what i understand from you’re previous post:

              <?php
              //This line load the package captcha created with build.transport.php
              //If there are maps file located in the model directory, they will be loaded too so you can have access
              //To your table with xPDO.
              $modx->addPackage('captcha',$captcha_core_path.'model/');
              
              //This line load the class called veriword.class.php located in the model directory
              //This is not directly related to xPDO.
              $modx->loadClass('captcha.VeriWord',$captcha_core_path.'model/',true,true);
              


              The maps files that you can create will be located in the model directory.
              When you load the package you can also precise the prefix of your custom table with the third option:

              <?php
              $this->modx->addPackage('yourpackage',$this->config['model_path'],'yourprefix_');
              



                • 28215
                • 4,149 Posts
                Quote from: BobRay at Nov 02, 2009, 06:14 AM

                How does loadClass() fit into this picture (assuming that it does)?
                I would have thought you’d have to load a class before you could uses getObject(), new, or newObject() on it.
                It think it would help if I knew what or where the class and package are being loaded or added *to*.
                Part of my confusion stems from the fact that captcha.php has this code in it (I think you know how it got there wink ):
                $modx->addPackage('captcha',$captcha_core_path.'model/');
                $modx->loadClass('captcha.VeriWord',$captcha_core_path.'model/',true,true);

                Is one or the other of these lines not needed or do they do different things?
                (I hope this isn’t hijacking the thread -- I’m confused enough that I can’t tell).
                Nope, here, loadClass specifies that it’s a transient class - ie, it’s not directly related to a DB table. So we use loadClass, rather than newObject, because it’s not loading an xPDOObject-based object - just a transient, helper class.

                newObject allows you to load xPDOObject-based objects (or custom-loader based objects, but that’s a whole ’nother topic) from the package maps and classes. It’s not used for transient classes, or standalone classes, such as above.
                  shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • Thanks for the input. Question: are you considering the XML schema your model? Could you please give an exact example? I’m sorry I’m so dense, but my mind can imagine that command pointing to several locations and working in several ways...

                  Does the following example load an XML file in /core/model/schema/? e.g. /core/model/schema/mypackagename.mysql.schema.xml? Or is it targeting the PHP files that reference that XML file?
                  $modx->addPackage('mypackagename','/path/to/my/model/');


                  If it’s referencing the XML, is it assumed that the format of the file name is "mypackagename.mysql.schema.xml"? If so, how do you change the database component (e.g. to Oracle or Mongo DB)?

                  And:
                  // ie, for a package "test" in your "core/components/myapp/model/test/'
                  $modx->addPackage('test',$modx->getOption('core_path').'/components/myapp/model/');


                  Again, does that command load up a PHP or an XML file?
                    • 28215
                    • 4,149 Posts
                    Quote from: Everett at Nov 02, 2009, 03:50 PM

                    Thanks for the input. Question: are you considering the XML schema your model? Could you please give an exact example? I’m sorry I’m so dense, but my mind can imagine that command pointing to several locations and working in several ways...
                    Well first you’ll need to parse the XML file and generate the object maps and class files:
                    http://svn.modxcms.com/docs/display/xPDO20/Generating+the+Model+Code


                    Does the following example load an XML file in /core/model/schema/? e.g. /core/model/schema/mypackagename.mysql.schema.xml? Or is it targeting the PHP files that reference that XML file?
                    $modx->addPackage('mypackagename','/path/to/my/model/');

                    It’s targeting the PHP classes and maps.


                    If it’s referencing the XML, is it assumed that the format of the file name is "mypackagename.mysql.schema.xml"? If so, how do you change the database component (e.g. to Oracle or Mongo DB)?
                    It can technically be whatever you want - we’ve just standardized that so people dont get confused.

                    xPDO at this current time doesnt support other DB platforms; it will in the future. Once we get that up and running, we’ll provide more info on how to do that.



                    // ie, for a package "test" in your "core/components/myapp/model/test/'
                    $modx->addPackage('test',$modx->getOption('core_path').'/components/myapp/model/');

                    Again, does that command load up a PHP or an XML file?
                    It parses the PHP maps and classes.
                      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • Thanks Shaun, I’ll work with this. Any ideas why the # of Confluence users has maxed out? I wanted to add a couple clarifications to the articles about this.