We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    Is there a best practice for querying the modx database for custom tables? Say I create a table called modx_jobs and there was were several fields. What would be the best way to query all the fields and display the results. With Evo I was using to

    $modx->db->select("*", " modx_jobs", "" , "date DESC" , "20")




    Thanks.
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 22303 MODX Staff
      • 10,725 Posts
      The DBAPI is deprecated in Revolution. Use PDO, which is what the xPDO (and by extension the modX) class give you direct access to the methods of, e.g.
      <?php
      $stmt = $modx->query("SELECT * FROM modx_jobs ORDER BY date DESC LIMIT 20");
      if ($stmt) {
          while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
              // do stuff with each row of the result
          }
      }
      ?>


      See PDO for more information, or learn about how to create custom xPDO table classes to represent your data model and access them using the same OO API that the MODx core data model uses.
        • 25551 ☆ A M B ☆
        • 1,231 Posts
        Thanks OG, that’s helped a lot.

        I did read other posts about using custom tables etc and it did seem rather complex for what was once a simple task. I know why this is the road with REV, it was stated in the posts I read. That was a nice simple solution to display tables from the database, is there a simple solution to INSERT tables as well?

        I am creating a json feed shoutbox and using ajax, users will add a table to the DB. I had this working in EVO using INSERT but thought I would now try REV. What would I need to do to achieve a simple $_POST to INSERT solution using the new modx methods?

        I tried this and it did work...
        $modx->db->insert( $sendShout, "modx_shouts"); 


        is this the correct way to add values to the DB using the REV way?

        Ross
          Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
          AugmentBLU - MODX Partner

          BLUcart - MODX Revolution E-Commerce & Shopping Cart
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: rossco at Oct 16, 2010, 03:16 PM

          I did read other posts about using custom tables etc and it did seem rather complex for what was once a simple task. I know why this is the road with REV, it was stated in the posts I read. That was a nice simple solution to display tables from the database, is there a simple solution to INSERT tables as well?

          I am creating a json feed shoutbox and using ajax, users will add a table to the DB. I had this working in EVO using INSERT but thought I would now try REV. What would I need to do to achieve a simple $_POST to INSERT solution using the new modx methods?

          I tried this and it did work...
          $modx->db->insert( $sendShout, "modx_shouts"); 


          is this the correct way to add values to the DB using the REV way?
          It will work for now, but again, the DBAPI is deprecated in Revo and will be removed in a future release. That said, the DBAPI can easily be made an Extra in the future if you want to plan to continue to use it for your Extras. But I highly recommend creating an xPDO model in the future for your MODx Extras that use database tables, as this will make it easier to port those to other databases when the MODx core is available for them, not to mention will provide a consistent API into your Extra’s data that can be utilized by generic MODx Snippets and Plugins in the same way core data is (see getValue for instance, which allows you to get a column value from any xPDO table class, not just core MODx tables).
            • 3749
            • 24,544 Posts
            Quote from: rossco at Oct 16, 2010, 03:16 PM

            I did read other posts about using custom tables etc and it did seem rather complex for what was once a simple task. I know why this is the road with REV, it was stated in the posts I read. That was a nice simple solution to display tables from the database, is there a simple solution to INSERT tables as well?
            Ross
            The typical way to do this is to create the tables in PhpMyAdmin, use writeSchema() to write a schema for them, then use parseSchema to create all the class and map files that will let you use xPDO with them.

            I’m working on a simplified tutorial on this for people in your situation, but it may be a while before it shows up.
              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
              • 25551 ☆ A M B ☆
              • 1,231 Posts
              Thanks for the replies.

              Yeah it would be great to see a simplified tutorial for this, coming from the ease of EVO to REV has been a slight shock. I’ve never worked with PDO before and it’s exceptionally complex compared to what I’m use to. I have always manually created the tables in the database that I use my custom snippets with and used the EVO methods to send and retrieve the data, it was so straight forward. I guess there’s a lot of reading and frustration ahead grin

              Looking forward to the tutorial Bob, I’m sure there’s a lot of us waiting... no pressure tongue
                Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                AugmentBLU - MODX Partner

                BLUcart - MODX Revolution E-Commerce & Shopping Cart
                • 3749
                • 24,544 Posts
                Quote from: rossco at Oct 17, 2010, 05:24 AM

                Thanks for the replies.

                Yeah it would be great to see a simplified tutorial for this, coming from the ease of EVO to REV has been a slight shock. I’ve never worked with PDO before and it’s exceptionally complex compared to what I’m use to. I have always manually created the tables in the database that I use my custom snippets with and used the EVO methods to send and retrieve the data, it was so straight forward. I guess there’s a lot of reading and frustration ahead grin

                Looking forward to the tutorial Bob, I’m sure there’s a lot of us waiting... no pressure tongue

                I can promise you, once you get the hang of xPDO in Revo, it’s *way* more straightforward than what you’ve been doing. smiley

                It looks incomprehensible at first, then all of a sudden, it all seems obvious.
                  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
                  • 25551 ☆ A M B ☆
                  • 1,231 Posts
                  I hope it’s as easy as that! What resources did you use to get yourself in-tune with it? Did you read the PDO manual for PHP or should the modx xPDO manual be enough?
                    Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                    AugmentBLU - MODX Partner

                    BLUcart - MODX Revolution E-Commerce & Shopping Cart
                    • 3749
                    • 24,544 Posts
                    Quote from: rossco at Oct 18, 2010, 07:26 AM

                    I hope it’s as easy as that! What resources did you use to get yourself in-tune with it? Did you read the PDO manual for PHP or should the modx xPDO manual be enough?

                    I never looked at the PDO manual at all (and know almost nothing about it), I just read the MODx online docs and asked a lot of stupid "how do I" questions. tongue

                    It was a challenge, but it was a piece of cake compared to the permissions. wink

                      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
                      • 25551 ☆ A M B ☆
                      • 1,231 Posts
                      Is there a tutorial on how to insert simple form data in to the modx database yet? I am about to start a project that will require data capture from forms in the database and would like to find out how best to send the input data for storage.

                      Thanks!
                        Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                        AugmentBLU - MODX Partner

                        BLUcart - MODX Revolution E-Commerce & Shopping Cart