We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7966
    • 90 Posts
    I upgraded to 2.1.3-pl from 2.0.8 version.

    I am changing the $modx->db->select calls to $modx->db->query in a snippet but I get the following error, when I run the this code.

    PHP Fatal error:  Call to a member function query() on a non-object in ......modx/core/cache/includes/elements/modsnippet/8.include.cache.php on line 42 


    The code on error line is

    $res = $modx->db->query("select id FROM `modx`.`modx_org_access_actions`");



    I also tried adding the "global $modx;" but that doesn’t help.

    Any ideas what could be the problem?

    Thank you
      • 3749
      • 24,544 Posts
      The db->query() methods have been deprecated for a while and are now gone in the latest versions. You can use PDO, or you can rewrite it using xPDO. There’s some information on that here:

      http://bobsguides.com/revolution-objects.html
        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
        • 7966
        • 90 Posts
        Quote from: BobRay at Jul 25, 2011, 04:13 AM

        The db->query() methods have been deprecated for a while and are now gone in the latest versions. You can use PDO, or you can rewrite it using xPDO. There’s some information on that here:

        http://bobsguides.com/revolution-objects.html

        Thanks Bob.

        I haven’t noticed that - modX->query() is the new function for $modx->db->select - and mixed it modx->db->query()

        For those not familiar with xpdo like me, below are some functions that might be helpful.

        OBSOLUTE --> $res = $modx->db->select($selectSQL , $fromSQL, $whereSQL , $OrderBy);
        NEW --> $res = $modx->query("select * from table Where field1 =’a’");

        OBSOLUTE --> $recordCount= $modx->db->getRecordCount($res);
        NEW --> $recordCount = $res->rowCount();

        OBSOLUTE --> $rows = $modx->db->makeArray( $res );
        NEW --> $rows = $res->fetchAll();

        OBSOLUTE --> $row = $modx->db->getRow($res);
        NEW --> $row = $res->fetch();
          • 36483
          • 11 Posts
          Very helpful, thanks smiley

          Here is an example of code so you can get back to work. This change prevented me from developing new stuff for like 2 weeks. Enjoy, world.

          $results = $modx->query("SELECT * from your_table");
           
          $recordCount = $results->rowCount();
          echo $recordCount;
           
          foreach($results as $row)
          {
              echo $row['test'];
          }
          [ed. note: andrewnormore last edited this post 15 years, 1 month ago.]