We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24629
    • 370 Posts
    Hi all,
    Here's the deal. I'm trying to couple stores with products.
    I have 3 tables 'products' , 'stores' and 'stores_products'. I have a form that updates the data for a store and i also want to store which products are available in a certain store. I'm planning to put that in the table 'store_products' (store_id and product_id);
    I made the Xpdo classes with Bob Ray's CreateXpdoClasses.and that all seems to work fine
    Now i can create and update records in 'Stores' with a form. (all code here is simplified for clarity)

    $store = $modx->newObject('Stores');
    $store->set('store_name', $_POST['store_name']);
    $store->save();


    in that form i also have some checkboxes for products so i can select which products are available in that store.
    <input type="text" name="store_name" id="store_name" value=""/>
    <input name="Agenda" id="Agenda" value="4" type="checkbox"/>



    Now how can i do an update or insert in the table 'store_products' that links the products to the store?
    i could make a Xpdo object for that:
    $entrie = $modx->newObject('Storesproducts');
    $entrie->set('product_id', $_POST['product_id']);
    $entrie->set('store_id', $_POST['store_id']);
    $entrie->save();


    But then i wouldn't now how to prevent duplicate entries in the table 'stores_products'? what is the best way of doing something like this?
    I really hope i made myself clear. i find it difficult to explain.

    Rdg

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      For one thing, I would presume that your stores_products table would have an index comprised of the two fields; this would be a unique index, so duplicates would throw an error.

      I'm not sure if a "joining" table like this should be a separate object. I'm not sure why I get that feeling, so hopefully somebody stronger in xPDO will be able to confirm or refute that feeling.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 24629
        • 370 Posts
        Susan,

        thats the tip i was looking for, a unique index comprised of the 2 values. thank you very much.
        i'm going to give it a try. But i agree i have a feeling there is probably a 'cleaner' way of doing this with Xpdo

        Rdg