88

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.

6
  • Just off the top of my head (ie untested), have you tried $f = clone $e? You may need to implement the __clone() method
    – Phil
    Jan 30, 2012 at 21:36
  • 4
    @Phil: cloned entity has the same PK, thus just updates the same row. And even more surprising - spl_object_hash (Doctrine uses it to identify particular instances) are the same for the original and the cloned object even though they contain different data
    – zerkms
    Jan 30, 2012 at 21:39
  • @Phil: __clone() wouldn't help either - Doctrine uses $oid = spl_object_hash($entity); and some internal map to get the state of the object. And for both (the original and cloned one) it would be the same - MANAGED
    – zerkms
    Jan 30, 2012 at 21:43
  • 1
    that's not true. clone $e returns another instance and thus, another spl_object_hash() value. Aug 22, 2013 at 12:52
  • 1
    Tried and was sure of that anyway. A clone is a different instance, and until you ask the UnitOfWork/ identityMap to register it, this entity will be considered to be INSERTed. Aug 23, 2013 at 7:28

4 Answers 4

185

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);
12
  • 9
    When I tried this method, any changes to the original entity were also persisted to the DB, as well as a new record being inserted. This is because the entity manager is still managing it. Even when I detach the original entity, it still persists the changes. I don't know why, or how to successfully get the entity manager to stop managing the original entity (i.e. discard the changes). Neither $em->refresh($old) or $em->detach($old) seem to work... Jul 14, 2014 at 21:37
  • 2
    If you are going to implement __clone() be sure to do so safely, as shown in the documentation otherwise you could easily break things. Feb 3, 2015 at 5:16
  • 2
    Please be aware that implementing the __clone() method only works in case of having flat objects without any relations. In particular only data that aren't references to other objects will be copied, all references will be the same as in the original object. Aug 11, 2016 at 6:49
  • 3
    @barbieswimcrew correct. I have another answer dealing with that scenario
    – Phil
    Aug 11, 2016 at 7:00
  • 3
    @ChadwickMeyer In case anyone else has this issue just flush your $new entity. $em->flush($new); NOT $em->flush(); otherwise both entities will save with the new data.
    – Jackson
    Jan 11, 2019 at 18:12
1

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

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

-2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.