Edit on GitHub

Cloning Elements

Use

$new = Pimcore\Model\Element\Service::cloneMe($source)

to get a safe copy of the original element (everything that implements ElementInterface). Note that this will not update any internal references. For example: A relation inside the source element pointing to the source element will still reference the source element in the copy.

If you want to get a persistent copy use the copyAsChildmethod of the corresponding service. E.g.

Pimcore\Model\Asset\Service::copyAsChild($target, $source)

where $sourceis the source element and $target the parent element of the new element. This will also create a unique element key (or filename for asset elements).

If you also want to update the references there is a helper method which accomplishes this for you. Just call the service's rewriteIds and provide a mapper config.

Example:

$rewriteConfig = array(
    "object" => array(
        176 => 190
    )
);
$object = DataObject\Service::rewriteIds($object, $rewriteConfig);

meaning that in the copy everything point to object with ID 176 will be replaced with a reference pointing to object 190.