We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36483
    • 11 Posts
    Hi all,

    I've been using MODx for about a year, and got very good with it. I had no problems designing software services and doing some pretty complex things. That was on the old API it seems. With the 2.1 removal of many features, I'm left in the dark as to how MODx now operates.

    MySQL queries?

    It seems as though in order to simply read from the database, I have to create an XPDO map? Then I have to create an object, call it, and... I'm a bit confused here. The documentation I've been reading here: http://rtfm.modx.com/display/revolution20/PHP+Coding+in+MODx+Revolution,+Pt.+I seems to be laughing at me. "Simple, Huh?".

    Can some one please give me a tutorial on how I can create a custom table in the MySQL database, and draw records from it?

    How do I...

    $result = $modx->db->query("SELECT * FROM my_test WHERE id=3);
    while($row = $modx->db->get_row_count($result))
    {
        echo $row['entry'];
    }



    Can some one simply explain this to me?

    THANKS! smiley
      • 21257 MODX Staff
      • 730 Posts
      Here are a few links that I hope give you the help you need

      http://rtfm.modx.com/display/revolution20/Summary+of+Legacy+Code+Removed+in+2.1
      This documents what was removed in 2.1 and gives pointers on what you might do to update your code.

      You don't have to create an xPDO schema just to read from a table. xPDO wraps PDO itself, and so all database functions you can call with PDO, you can call with xPDO. Where the document above refers to xPDO->query(), you'll find more details in the xPDO API docs
      http://api.modx.com/revolution/2.1/

      and in the PDO docs
      http://php.net/manual/en/book.pdo.php
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 3749
        • 24,544 Posts
        If the custom table(s) already exist, you might find this helpful: http://bobsguides.com/custom-db-tables.html.

        There is also some info here about using xPDO with MODX objects: 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
          • 36483
          • 11 Posts
          Thanks for the help guys, really looking forward to becoming a MODx pro again.

          Just so you know Bob, you've helped a LOT of people I know. I started out with your tuts, and the guys I used to work with who chose MODx as their corporate CMS really used your material as well.

          I'll keep you posted on any problems I find! THANKS so much!!!
            • 36483
            • 11 Posts
            Hey Prohpet, could you provide me an example of a xPDO query?

            Hey Bob,
            Seems like if I make my database table, I have to run a resource that creates that schema file. What if I change a database structure? I have to run that resource again? That really seems like a pain in the arse... :S

            So looking at this page: http://rtfm.modx.com/display/xPDO20/Database+Connections+and+xPDO

            It seems I need to create an xPDO connection? That means I have to include the snippet that does this, on every page template? I'm so confused as to how get this working!! [ed. note: andrewnormore last edited this post 15 years, 1 month ago.]
              • 36483
              • 11 Posts
              So here was the help I was looking for:

              $results = $modx->query("SELECT * from your_table");
               
              $recordCount = $results->rowCount();
              echo $recordCount;
               
              foreach($results as $row)
              {
                  echo $row['test'];
              }


              This is PDO, or xPDO? I'm confused on those two terms.
                • 3749
                • 24,544 Posts
                You don't need to create an xPDO connection -- MODX already does that, though you do need to load your classes before using them.

                You would need to recreate the class and map files if you alter your table. xPDO assumes that you design the table the way you want it to begin with. wink


                Your code above is PDO. It will work fine, but you won't get the advantages of xPDO.
                  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