We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27889
    • 415 Posts
    I’m trying to duplicate an object (document) with its related items (document groups and tvs values), but don’t know the better way to do this.

    to copy the document :
    <?php
    $object= $xpdo->getObject('sitecontent',$model);
    $object->_new=true;
    $pkname=$object->getPK();
    $object->_fields[$pkname]='';
    $object->save();
    ?>


    get all related items of $model with getOne and getMany and foreach:
    - empty their PK
    - set their _new fields to true
    - set their foreign key accordingly to the new PK of the document created
    - save them
    Is the above method a good one ?
    I having difficulty to understand how related items really works, when they have loaded in the object and how avoid recursive call when parsing them.

    Thanks for your works and your time.
      MODx Sites & Prestations: http://dp-site.fr [Last MODx Site]
      MODx Repository: [HOME] [MetaTagsExtra] / Current Dev: [xFDM]
    • There is currently no support for deep cloning; but would make a great additional feature. For now, you will need to deal with each object individually, like so...

      $object= $xpdo->getObject('sitecontent',$model);
      $newobj= $xpdo->newObject('sitecontent');
      $newobj->fromArray($object->toArray());
      $newobj->save();


      The implementation of deep cloning should be fairly trivial, simply looping through any related objects and performing similar fromArray() calls on new instances.