We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38593
    • 129 Posts
    I am trying to bring a MySQL query into xPDO for use in a MODX snippet, but alas the documentation assumes much more reader expertise than I have.

    I have two tables within the database, Products (product number, name, description) and Stock (stock number, product number, size, color, price). I am trying to query the Stock table using a stock number to fetch size, color, price and product number (all of which will be set as MODX placeholders and inserted in a chunk). I also need to use the product number supplied in the query to do a second query to the Products table to fetch name and description.

    I understand my start should be:

    $dsn = 'mysql:host=localhost;dbname=store;charset=utf-8';
    $xpdo = new xPDO($dsn,'myusername','mypassword');
    
    $xpdo->newQuery('Products');


    From here I don't understand. Do I use a getObject to fetch the specific row for the stock number? Or do I work with where(array())? Or do I add array to the newQuery('Products', array('stocknumber' = '123xyz'))? Or?

    Then once I have the query results do I use get('price'), get('color'), etc? Or is there a different method?

    Sorry for the mickey mouse nature of these questions, but the MODX documentation is lacking in simplified beginners info.
      Pedalers Bicycle Tours my website forged with MODX
      vandergraaf-M static website generator for MODX
      • 3749
      • 24,544 Posts
      FYI, since the modX class extends the xPDO class, you can call any of the methods with $modx->methodName().

      First, your custom tables have to be set up to work with xPDO.

      If your tables are in the MODX database but with a different table prefix (both highly recommended), you don't need the dsn specification and you don't need to instantiate $xpdo, since $xpdo will already exist and be connected.

      If you haven't set the tables up for xPDO, this might help: http://bobsguides.com/custom-db-tables.html, although if your tables are related (which it looks like they are) you'll have to edit the schemas manually to specify the aliases before generating the class and map files (though you don't have to do that right away, you can still get objects from either table with xPDO after running the script on the page in the link).

      There's a deeper explanation here: http://rtfm.modx.com/revolution/2.x/case-studies-and-tutorials/using-custom-database-tables-in-your-3rd-party-components.

      You'll also need to load your classes with loadClass() before calling any xPDO methods for them.

      It's not mandatory, but if it's not too late, I'd recommend that instead of a stock number and a product number, you use a primary key, autoincrement field called 'id' for both. If your tables exist and have a lot of products in them, at least make product number and stock number the primary keys.

      I haven't really answered your question, but without the steps above, an answer wouldn't help you.

      There's an introduction to getting objects with xPDO here: http://bobsguides.com/revolution-objects.html

      I hope this helps more than it confuses you. It's not as difficult as it sounds. 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
        • 38593
        • 129 Posts
        Thanks, I see I have some reading ahead of me.

        Our database tables exist already and are being used by our current non-MODX non-CMS website via old fashion PHP/MySQL queries. And as they contain lots of data, I am not too keen of creating and repopulating new tables specifically for MODX's XPDO. Plus once I get this sorted, there are a number of other db tables, that feed different aspects of the website that I will also have to build xPDO queries for.

        A wise businessman would hire someone to do this for him. But I like to try and learn as I go, which sometimes results in sheer frustration.
          Pedalers Bicycle Tours my website forged with MODX
          vandergraaf-M static website generator for MODX
          • 3749
          • 24,544 Posts
          On the plus side, if you do it yourself, you'll know what makes it tick. wink

          As for your DB, I wouldn't suggest re-doing it from scratch, but you might consider moving the tables inside the MODX DB, which can be done in a few minutes with PhPMyAdmin. It will make things *much* faster and simpler, though you'd have to do a little rewriting in your other app.

          I would rename them (before or after the move) by giving them a prefix (e.g., products_), then simply "Export" them (which sends them to an SQL file), and in the MODX DB "Import" them and select the SQL file.
            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