Doctrine 2: how to clone all values from one object onto another except ID?

后端 未结 3 1304
臣服心动
臣服心动 2020-12-30 22:10

In $entity variable, there is an object of same type as $other_address, but with all field values filled in.

I want to set all fields in $other_address object to hav

相关标签:
3条回答
  • 2020-12-30 23:00
    $A = $em->find('Some\Entity',1);
    
    $B = clone $A;
    $em->persist($B);
    $em->flush();
    

    if you merge it will update the entity, best you use persist() it will duplicate the entire row and add auto incremented primary key

    0 讨论(0)
  • 2020-12-30 23:08

    Just Clone the entity, you don't even need to unset the id. Doctrine has tackled this for you

    0 讨论(0)
  • 2020-12-30 23:13

    I'm not sure why cloning won't work.

    This seems to work for me, at least in a basic test case:

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

    If you've got relationships to worry about, you might want to safely implement __clone so it does what you want it to do with related entities.

    0 讨论(0)
提交回复
热议问题