We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10473
    • 44 Posts
    Is it possible to just write an SQL statement in 2.1 without having to create XPDO objects and classes and whatnot?

    I miss $modx->db more than words can say.
      • 24068
      • 4 Posts
      Quote from: garethwi at May 27, 2011, 05:52 AM

      Is it possible to just write an SQL statement in 2.1 without having to create XPDO objects and classes and whatnot?

      I miss $modx->db more than words can say.

      You could use the pdo-instance $modx->pdo (http://php.net/pdo)

      But i can not recomend it.

      EDIT:

      have a look at http://php.net/pdo.query and http://php.net/pdo.exec

      $sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
      foreach($modx->pdo->query($sql) as $row) {
          print $row['name'] . "\t";
          print $row['color'] . "\t";
          print $row['calories'] . "\n";
      }
      
        • 28215
        • 4,149 Posts
        Quote from: pureppl at May 27, 2011, 06:45 AM

        Quote from: garethwi at May 27, 2011, 05:52 AM

        Is it possible to just write an SQL statement in 2.1 without having to create XPDO objects and classes and whatnot?

        I miss $modx->db more than words can say.

        You could use the pdo-instance $modx->pdo (http://php.net/pdo)
        $sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
        foreach($modx->pdo->query($sql) as $row) {
            print $row['name'] . "\t";
            print $row['color'] . "\t";
            print $row['calories'] . "\n";
        }
        


        Erm, I recommend just using $modx->query() if you’re going to do that, since modX extends xPDO, which has all PDO methods:

        $sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
        $stmt = $modx->query($sql);
        if ($stmt && $stmt instanceof PDOStatement) {
          while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            print $row['name'] . "\t";
            print $row['color'] . "\t";
            print $row['calories'] . "\n";
          }
        }
        


        Is the more proper code.

          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          At the risk of completely disgusting Jason, I’ll add that of course any time you want to you can use the raw PHP mysql database functions.

          Back when a database abstraction layer was being discussed (which led to the dbAPI for Evo), I wanted to use this library, as I had used it before and rather liked it (WordPress uses it). http://justinvincent.com/ezsql
            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
            • 22303 MODX Staff
            • 10,725 Posts
            You’re not disgusting me Susan, but if you choose to use these functions in Revolution, you will always create two database connections, which is the most expensive part of any PHP request. This simply makes no sense. There is a DBAPI plugin for Revolution if you need it, which will share the connection.

            The other benefits of Revo/xPDO will also be useless, such as database result set caching, which is a huge feature for scalability of large sites or when working with external database servers. Future features like master/slave support for write/read operations respectively, will also not be any use if you choose to circumvent the Database Access layer which MODX Revolution is built upon. I won’t even mention supporting multiple database engines with a single codebase.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Indeed, if I ever do energize myself to get into Revo, it will definitely be mostly because of the elegant power of xPDO. I wasn’t recommending using raw PHP mysql functions, just saying that one could if one really wanted to for some reason. But it’s definitely worth the effort to learn to be comfortable with the xPDO API if you’re working with Revo.
                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
                • 3749
                • 24,544 Posts
                If you’re just querying the MODX tables, xPDO is not all that difficult: http://bobsguides.com/revolution-objects.html, and it’s definitely worth learning if you’re running Revolution.

                If you need to deal with your own DB tables, you can autocreate the xPDO classes and maps: http://bobsguides.com/custom-db-tables.html and use the same methods on the page above for them.
                  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