We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    xPDO is just a wrapper for PDO the provides a way to map classes to database tables. PDO is a PHP standard for accessing databases through a common interface. If you are familiar with PDO, you can use xPDO exactly as you would PDO. In fact, the query() and fetch() methods above are part of PDO and PDOStatement; nothing unique to xPDO.

    Start with the PDO documentation and then you can start to explore the realm of object/relational mapping xPDO adds on top of it once you better understand the database access layer.
      • 10525
      • 247 Posts
      Yes I've had a look at this PHP documentation, and it's not the clearest either (for example, it uses "$sth = $dbh->prepare(...)" in its examples, but never states what $dbh is): http://uk3.php.net/manual/en/pdo.prepare.php.

      What I want to know is how "$results = $modx->query($sql);" relates to PDO. Do I just replace the mysterious $dbh with $modx and adapt the PHP examples, or is there any MODx-specific documentation on the use of these functions with $modx?

      To reiterate, at the moment for the purposes of fast development I am trying to avoid the "realm of object/relational mapping" and use the simplest query methods possible.
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        I think there are several relevant examples on http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.query
          • 10525
          • 247 Posts
          Thanks Everett, I had a look at that page too, and it's not bad. But I saw from discussions elsewhere that there's a MODx bug which results in an empty result set still being returned as an object (February 2013, still unfixed: http://tracker.modx.com/issues/9502), so the first example on that page is allegedly wrong.

          Am I correct in surmising that $modx is a PDO/xPDO object and has access to all the functions that that type of object has?

          I have come across this tutorial which helps enormously: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            $xpdo (and thus $modx) implement most of PDO, but the best way to figure it out is playing around with it -- I did that and and added the examples to that page. I found that most PDO things were supported -- the only thing I remember not being supported all the time was method chaining (thus the note re "No One-Liners! ").

            Re the bug -- that's a bit annoying, I agree... PHP quirks:

            $x = new stdClass(); // an empty object
            if (empty($x)) {
                // an "empty" object is never considered empty :(
            }
            


            You could test your results like this I guess:

            $x = new stdClass();
            $y = (array) $x;
            if (empty($y)) {
                print 'No results.';
            }
            
              • 32699 ☆ A M B ☆
              • 427 Posts
              As previously mentioned, the best way is to simply do it. Once you get out of the Mysqli / Mysql way of handling databases and use xPDO (PDO) you will never look back. Code reduction is as much as 80%.

              Take a look at this: http://www.shawnwilkerson.com/modx-revolution/2012/03/10/programmatically-working-with-the-moduser-object/

              It plays with the modUser object.

              Study the modUser portion of the schema paying attention to the aggregate and composite relationships.

              Also look at https://forums.modx.com/thread/85383/how-do-i-set-contents-of-external-db-to-custom-placeholders#dis-post-471191 for a cheat snippet to get a schema parsed.

              Remember, it should be located in: /core/components/PACKAGE_NAME/model/schema

              And named (in all lower case): PACKAGE_NAME.mysql.schema.xml

              The primary barrier most people I have helped with xPDO is they are over-thinking and complicating the xPDO process. It actually is pretty straight forward once you understand you don't really need to think of the database and data as separate entities any longer.

              xPDO allows developers to think of primarily the data and only passively the database.

              To better explain:

              I have a client table (for brevity we will just use the modUser).

              Once I defined and created from xPDO,I typically only have to think user.... What do I need or want from it. I don't have to think so much of how do I get it. I already did that in the schema - once and for all.

              I have yet to move my early experiments of xPDO to my site. I'll see if I can find a couple of articles and get them up there.

              The official docs are good - once you have an idea of what you are doing.

              Maybe I need to build a beginners guide on my site.





              [ed. note: wshawn last edited this post 12 years, 7 months ago.]
                Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com