0

I am writing a duplicate action within Symfony and Doctrine..

What I want to accomplish is to just load the object, unset the id and persist it again.

I was thinking of an example:

$A = $em->find('Some\Entity',1);
$B = clone $A;
$B->setId(null);

Is this the right way to do that?

Idea is to have an /duplicate endpoint which will implement this logic. I saw that using __clone() in the entity class would be the best way to go, but is there any workaround for my case?

  • [This](https://stackoverflow.com/questions/14158111/deep-clone-doctrine-entity-with-related-entities) might help you.. – Bossman Sep 29 '22 at 10:56
  • I updated my post. Can you please take a look? Thanks @Bossman – Filip Stojavonic Sep 29 '22 at 11:01
  • 2
    So, if you create a `__clone` method in your entity that sets the `id` as null (This gets invoked when you use `clone`). Then in your `/duplicate` endpoint do what you are doing `$new = clone $A` and persist as normal, it should add as a new entity with a new *id*. – Bossman Sep 29 '22 at 11:24
  • I think you can use the detach method from doctrine too – Charlie Lucas Sep 30 '22 at 13:37

0 Answers0