We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34120
    • 236 Posts
    I’m writing a snippet which connects to the modx db and queries some custom tables I’ve created. The snippet contains some rowboat stuff which works fine but I’ve had to write some queries using joins so I’ve gone and written them in straight up PHP. I can’t get my head around the XPDO stuff at the moment so I’m just using what I know. The problem is I’ve created my own connection but I’ve read that this is a bad idea, multiple connections, you are already connected to the DB within a snippet.

    So how do I write my queries to use the modx connection?

    $result = @mysqli_query($myconnection, $sql);


    Thanks

    MODX Revo 2.1.0-pl (traditional)
      • 33968
      • 863 Posts
      I may be wrong but I think you’ll have to use xPDO from Revo 2.1 onwards. Here’s a list of deprecated methods which includes all the $modx->db methods.

      Do you have an xPDO schema and class set up for your table? If you haven’t got that far it’s really worth spending a bit of time to figure out how it works... have a look at this for starters: http://bobsguides.com/custom-db-tables.html
        • 34120
        • 236 Posts
        Thanks for the reply lucas, yeah I think that sounds about right. I was kind of hoping there would be a quick fix so I could just replace $myconnection with the modx connection.
        I have’t set any xPDO schema or classes, it’s a bit late in the day for me to be doing that. I’ve dabbled with it before but found it fairly mind bending.

        I’ll have to run it off my own connection for now.
          • 34120
          • 236 Posts
          So I’ve setup schema and classes for my tables, that was pretty straight forward thanks to bob’s guide. Could anyone help me out with converting my query to xPDO it contains a couple of inner joins? I’m currently looking over various documentation and articles but a crash course would be really helpful. Maybe I should make a fresh post in the xPDO forum.

          I’ve also got a quick and probably embarrassing question on basic fundamentals:
          What is the difference between:
          $xpdo->addPackage
          and
          $modx->addPackage
            • 34193
            • 330 Posts
            Quote from: rf9 at Jun 15, 2011, 06:22 AM

            So I’ve setup schema and classes for my tables, that was pretty straight forward thanks to bob’s guide. Could anyone help me out with converting my query to xPDO it contains a couple of inner joins? I’m currently looking over various documentation and articles but a crash course would be really helpful. Maybe I should make a fresh post in the xPDO forum.

            I’ve also got a quick and probably embarrassing question on basic fundamentals:
            What is the difference between:
            $xpdo->addPackage
            and
            $modx->addPackage


            $modx extends $xpdo so has all the functionality of $xpdo plus some.

            If using modx change $xpdo to $modx anywhere it appears.

            xpdo can be used with out modx as a sperate entity thats why the docs use $xpdo but when inside modx always use $modx.
              • 33968
              • 863 Posts
              Quote from: rf9 at Jun 15, 2011, 06:22 AM

              Could anyone help me out with converting my query to xPDO it contains a couple of inner joins?
              Sure, post your query and we’ll take a look!
                • 34120
                • 236 Posts
                Thanks pete, that’s a great help. I’ve wondered this for a while smiley

                Here’s my query:
                $sql = "SELECT P.product_id, P.title AS product, 
                               V.description AS variation,
                	       PV.product_code AS product_code,
                	       V.variation_id AS variation_id
                          FROM products as P
                	  INNER JOIN product_codes AS PV 
                               ON PV.product_id = P.product_id
                	  INNER JOIN variation as V
                	       ON V.variation_id = PV.variation_id
                	  WHERE P.product_group_id = $productGroupId
                	       ORDER BY P.product_id "; 
                  • 22303 MODX Staff
                  • 10,725 Posts
                  You don’t have to convert anything. Just execute it as you would any query with PDO and work with the PDOStatement that is returned...

                  <?php
                  $stmt = $modx->query("SELECT P.product_id, P.title AS product, 
                                 V.description AS variation,
                  	       PV.product_code AS product_code,
                  	       V.variation_id AS variation_id
                            FROM products as P
                  	  INNER JOIN product_codes AS PV 
                                 ON PV.product_id = P.product_id
                  	  INNER JOIN variation as V
                  	       ON V.variation_id = PV.variation_id
                  	  WHERE P.product_group_id = $productGroupId
                  	       ORDER BY P.product_id ";);
                  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                      // do stuff with the $row here
                  }
                  
                    • 34120
                    • 236 Posts
                    Thanks for the reply OpenGeek, I’m trying out your suggestion but getting "Call to a member function fetch() on a non-object" (edit: I think this was a sql error)

                    Prior to your post I was working on rewriting the query, this is how far I got:
                    $c = $modx->newQuery('Products');
                    $c->innerJoin('ProductCodes','PV','PV.product_id = Products.product_id');
                    $c->innerJoin('Variation','V','V.variation_id = PV.variation_id');
                    $c->where(array(
                       'Products.product_group_id' => 1,
                    ));
                    $c->sortby('Products.product_id','ASC');
                    
                    $products = $modx->getCollection('Products',$c);
                    $output .= '<p>Total: '. count($products) . '</p>';
                    
                    foreach($products as $product) {
                       $output .= '<p>Size imp: ' . $product->get('size_imp');
                       $output .= '<br />Code: ' . $product->get('product_code');
                       $output .= '<br />Author: ' . $product->get('description') . '<br /></p>';
                    }
                    return $output;

                    The vales from the main table are being displayed but not values from the joined tables. I think I need to be using getCollectionGraph but I haven’t quite figured it out yet.
                      • 34120
                      • 236 Posts
                      I’ve got my original snippet working by making a few tweaks based on your post OpenGeek.

                      Thanks for all the help guys, really appreciate it. I will revisit this topic when I have a bit more time on my hands.