How to re-save the entity as another row in Doctrine 2

62.1k views Asked by At

Let's say I have entity $e. Is there any generic way to store it as another row, which would have the same entity data but another primary key?

Why I need this: I'm implementing some sort of Temporal Database schema and instead of updating the row I just need to create another one.

5

There are 5 answers

12
Phil On BEST ANSWER

Try cloning and add the following method to your entity

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

You may need to detach the entity before persisting it. I don't have my dev machine handy to test this right now.

$f = clone $e;
$em->detach($f);
$em->persist($f);
$em->flush();

Update

Just tried using a simple SQLite demo. You shouldn't need to do anything. The following worked for me without adding a __clone() method or doing anything else out of the ordinary

$new = clone $old;
$em->persist($new);
$em->flush();

Once flushed, the $new entity had a new ID and was saved as a new row in the DB.

I would still null the ID property via the __clone() method as it makes sense from a pure model view.

Update 2

Digging into the Doctrine code, this is because the generated proxy classes implement __clone() with this important line

unset($this->_entityPersister, $this->_identifier);
0
user1077915 On

I just do:

/**
 * __clone
 *
 * @return void
 */
public function __clone()
{
    $this->id = null;
}

More details here https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/implementing-wakeup-or-clone.html

0
Onur Aydeniz On

clone and detach worked for me.Symfony version 5.4 does not accept any arguments for flush()

    $new = clone $discount;
    $new->setId(null);
    $discountRequest = new DiscountRequest();
    $discountRequest->setDiscount($new);
    
    $discountRequest->setOldDiscount($discount->getId());
    $entityManager->persist($discountRequest);
    $entityManager->detach($discount);
    $entityManager->flush();
0
Mike Smit On

Copying the data in a new Object of the same class and persisting it will do. Keep it simple!

1
viejalospelos On

Setting id to null inside the clone method returned me an error; I solved it using unset() instead

public function __clone() 
{
    unset($this->id);
}