We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Ok, I’m working through my CRUD operations using xPDO. I’ve got basic stuff working, but how does the save() method work when you’ve got a complex object? I.e. one with related objects?

    In my database, I have some forms which are designed to track a variable number of resources, so each TrackingForm object has multiple Resource objects. It’s waaay easier to manually go through all the children objects and save them one-by-one, but I’m wondering if there’s a more direct way. And does saving the parent object update/remove/replace all the children objects in the joining tables?

    This also brings into question how the data is best stored in the $_POST array...
      • 21257 MODX Staff
      • 730 Posts
      If the relationships are defined properly, then saving a parent object will save/update children. I’ve had it save a zany set of related objects 6 levels deep, so I know this works. And doing so, the sql INSERTS and UPDATES are optimized or at least the statements are prepared. (opengeek could comment more intelligently on how much of a performance impact that has).

      To delete a related object, you need to call ->remove() on it explicitly. Note that doing so will also remove any "child" objects (objects defined as having composite relationship to the one you’re removing). I’m making that distinction because if you were to just remove a related object from the parent by calling function like unset($Form->Resources[$x]), xPDO would not remove the related object on saving $Form.
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 9207 ☆ A M B ☆
        • 2,475 Posts