We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22656
    • 121 Posts
    Adding another abstraction layer to something which works already is unneded in my humble opinion.
    (Counter-argument to this would be hell why did we ever move away from assembly, or binary for that matter, grin)

    But hear me out. How many people do you think will find themself in a situation where they go: "hmmmm our mysql server doesnt fit our needs, lets move to mssql!" to justify using ORM in current modx’s incarnation. I would bet that most people would choose to use DBAPI interacting with their databases directly, they do not want to take the time and effort to learn another abstraction layer if they do not have to, including me. Particularly for majority of their projects which involve simple database reads with a few writes. I am sure there are users out there which love and value what XPDo brings to the table, but I would bet that majority of users would not choose to go down this road.

    For now, we have resorted to using native mysql functions in PHP, which means each snippet is storing database credentials - not good if anything changes. On evo this was great, you had access through DBAPI and could issue queries directly in your snippets.

    You could almost think of this in terms of how easy you want to make your product to general users - the easier it is for most people to do most things, the greater chance you have of them adopting this CMS (sorry, framework!) over its competitors.
      • 24068
      • 4 Posts
      Quote from: sottwell at Jun 18, 2011, 10:13 PM

      The biggest problem with SQL is that there is no standard, or at least there is supposed to be a standard but no one uses it. Every implementation has their own proprietary non-compatible versions of too many of the basic operations.

      The one big reason for mozilla to completly drop websql and implement indexedDb.

      http://hacks.mozilla.org/2010/06/beyond-html5-database-apis-and-the-road-to-indexeddb/
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: papachumba at Jun 21, 2011, 10:52 AM

        But hear me out. How many people do you think will find themself in a situation where they go: "hmmmm our mysql server doesnt fit our needs, lets move to mssql!" to justify using ORM in current modx’s incarnation. I would bet that most people would choose to use DBAPI interacting with their databases directly, they do not want to take the time and effort to learn another abstraction layer if they do not have to, including me. Particularly for majority of their projects which involve simple database reads with a few writes. I am sure there are users out there which love and value what XPDo brings to the table, but I would bet that majority of users would not choose to go down this road.

        For now, we have resorted to using native mysql functions in PHP, which means each snippet is storing database credentials - not good if anything changes. On evo this was great, you had access through DBAPI and could issue queries directly in your snippets.

        You could almost think of this in terms of how easy you want to make your product to general users - the easier it is for most people to do most things, the greater chance you have of them adopting this CMS (sorry, framework!) over its competitors.
        If you care nothing about scalability or backward compatiblity while we continue to improve and change MODX, then no, this layer is not necessary. And falling back to native mysql_ functions is causing you to use multiple database connections on your page requests, one of the most expensive operations PHP has to perform, especially if you are using a remote database servers. You can still execute SQL, take advantage of PDO prepared statements, and utilize xPDO’s database result set caching capabilities without learning a thing about the xPDO abstraction layer or using objects. The choice of using PDO, which is a standard database access layer in PHP is absolutely on purpose and xPDO provides direct access to PDO methods with some additional features that make it even more useful. I would learn more about PDO before saying the DBAPI is a better option. And if you still don’t agree, you can use the dbapi plugin to add it back to MODX 2.1. BTW, the DBAPI uses PDO in MODX Revolution; it does not use the native mysql functions as did the DBAPI in Evolution.
          • 22656
          • 121 Posts
          Thanks for explaining that Jason, and excuse my ignorance - I was under the impression DBAPI was the ONLY way to access my external dbases bar native PHP. Would you care to supply a little example snippet of code running a simple mysql query using PDO?

          thanks in advance
            • 34193
            • 330 Posts
              • 22656
              • 121 Posts
              I guess this is it, from splittingred:

              $stmt = $modx->query(’SELECT * FROM `mytable` ORDER BY `name` ASC’);
              if ($stmt && $stmt instanceof PDOStatement) {
              while ($row= $stmt->fetch(PDO::FETCH_ASSOC)) {
              echo $row[’name’];
              }
              }
                • 22303 MODX Staff
                • 10,725 Posts
                If you want to take advantage of xPDO result set caching (cache_db config option), you can also do it using the xPDOCriteria class (which is defined in the main xpdo.class.php file):

                <?php
                $criteria = new xPDOCriteria($modx, "SELECT * FROM `mytable` WHERE `food` = ? ORDER BY `name` ASC", array('beer'), true);
                if ($criteria->stmt && $criteria->stmt->execute()) {
                    while ($row= $criteria->stmt->fetch(PDO::FETCH_ASSOC)) {
                        echo $row['name'];
                    }
                }


                This shows using a prepared statement (’beer’ is the bound to the query where the ? appears when execute() is called on the PDOStatement that is created when you construct a new xPDOCriteria), and enabling the query to cached indefinitely (the last parameter with a value of true; you could also pass an integer indicating the number of seconds to cache the result set).

                All the methods of PDO are available directly though from the $modx (or an $xpdo) instance, as xPDO (and modX which extends xPDO) provides these methods with additional connect-on-demand features that delay getting a database connection and actual PDO instance until it is needed, rather than when xPDO is constructed.
                  • 4971
                  • 964 Posts
                  So XPDO extends PDO and modX extends XPDO?
                    Website: www.mercologia.com
                    MODX Revo Tutorials:  www.modxperience.com

                    MODX Professional Partner
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: charliez at Jun 22, 2011, 02:20 PM

                    So XPDO extends PDO and modX extends XPDO?
                    modX does extend xPDO, but PDO is actually stored as a member variable on an instance of xPDO/modX. This is what allows the connect-on-demand features, and in the future there will be support for multiple connections to be configured (e.g. for read/write isolation on master/slave database configurations), so technically you would say...

                    xPDO wraps PDO and exposes/overrides the behavior of it’s methods
                      • 17499 ☆ A M B ☆
                      • 872 Posts
                      Is it possible to use toSQL method with xPDOCriteria ?