Deep clone Doctrine entity with related entities

Doctrine OrmSymfony 2.1

Doctrine Orm Problem Overview


I have created an entity A with OneToMany relation to B, which have relation OneToMany to C.

I have to clone this A entity and set it in database with new id. Also all deep relations should be cloned with new ids too.

What have I tried is to set A id to null:

$A = clone $A_original;
$A->setId(null);
$em->persist($A);

It creates new record in A table, but does not in B and C.

What should I do to make a full copy of A entity ?

Doctrine Orm Solutions


Solution 1 - Doctrine Orm

You have to implement a __clone() method in your entities that sets the id to null and clones the relations if desired. Because if you keep the id in the related object it assumes that your new entity A has a relation to the existing entities B and C.

Clone-method for A:

public function __clone() {
    if ($this->id) {
        $this->setId(null);
        $this->B = clone $this->B;
        $this->C = clone $this->C;
    }
}

Clone-method for B and C:

public function __clone() {
    if ($this->id) {
        $this->setId(null);
    }
}

https://groups.google.com/forum/?fromgroups=#!topic/doctrine-user/Nu2rayrDkgQ

https://doctrine-orm.readthedocs.org/en/latest/cookbook/implementing-wakeup-or-clone.html

Based on the comment of coder4show a clone-method for a OneToMany relationship on A where $this->M is OneToMany and therefore an ArrayCollection:

public function __clone() {
    if ($this->id) {
        $this->setId(null);
        
        // cloning the relation M which is a OneToMany
        $mClone = new ArrayCollection();
        foreach ($this->M as $item) {
            $itemClone = clone $item;
            $itemClone->setA($this);
            $mClone->add($itemClone);
        }
        $this->M = $mClone;
    }
}

Solution 2 - Doctrine Orm

There is also a module that will do this called DeepCopy:

https://github.com/myclabs/DeepCopy

$deepCopy = new DeepCopy();
$myCopy   = $deepCopy->copy($myObject);

You can also add filters to customize the copy process.

Solution 3 - Doctrine Orm

I wasnt able to use DeepClone (it require php 7.1+), so I founded more simple way to clone relations in entity __clone method

$this->tags = new ArrayCollection($this->tags->toArray());

Solution 4 - Doctrine Orm

A clean way to clone a ArrayCollection:

$this->setItems(
    $this->getItems()->map(function (Item $item) {
        return clone $item;
    })
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhszView Question on Stackoverflow
Solution 1 - Doctrine OrmflecView Answer on Stackoverflow
Solution 2 - Doctrine OrmOnshopView Answer on Stackoverflow
Solution 3 - Doctrine OrmAndrew ZhilinView Answer on Stackoverflow
Solution 4 - Doctrine OrmDMatView Answer on Stackoverflow