Doctrine 原则2:如何将除ID之外的所有值从一个对象克隆到另一个对象上?

Doctrine 原则2:如何将除ID之外的所有值从一个对象克隆到另一个对象上?,doctrine,doctrine-orm,symfony,Doctrine,Doctrine Orm,Symfony,在$entity变量中,有一个与$other_address类型相同的对象,但已填充所有字段值 我想将$other_address对象中的所有字段设置为与$entity对象具有完全相同的值 这在少于N行的情况下可行吗,其中N是我需要设置的字段数 我尝试了“克隆”关键字,但没用 这是密码 $other_address = $em->getRepository('PennyHomeBundle:Address') ->findBy(ar

在$entity变量中,有一个与$other_address类型相同的对象,但已填充所有字段值

我想将$other_address对象中的所有字段设置为与$entity对象具有完全相同的值

这在少于N行的情况下可行吗,其中N是我需要设置的字段数

我尝试了“克隆”关键字,但没用

这是密码

                $other_address = $em->getRepository('PennyHomeBundle:Address')
          ->findBy(array('user' => $this->get('security.context')->getToken()->getUser()->getId(), 'type' => $check_type));
                $other_address = $other_address[0];


                //I want to set all values in this object to have values from another object of same type
                $other_address->setName($entity->getName());
                $other_address->setAddress1($entity->getAddress1());
                $other_address->setAddress2($entity->getAddress2());
                $other_address->setSuburbTown($entity->getSuburbTown());
                $other_address->setCityState($entity->getCityState());
                $other_address->setPostZipCode($entity->getPostZipCode());
                $other_address->setPhone($entity->getPhone());
                $other_address->setType($check_type);

我不知道为什么克隆不起作用

至少在一个基本的测试用例中,这似乎对我有效:

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

$B = clone $A;
$B->setId(null);

如果你有关系要担心,你可能会想让它做你想让它做的事情。只需克隆实体,你甚至不需要取消id。条令已经为你解决了这个问题

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

$B = clone $A;
$em->persist($B);
$em->flush();

如果您
merge
它将更新实体,最好使用
persist()
它将复制整行并添加自动递增的主键作为DavidLin点,您不必“取消设置”实体ID(如果字段正确标记为ID)HI@timdev,如果我想复制,假设我有一个临时对象,我需要将它合并到原始对象中?这可能吗?如果您使用自己的ID生成器,则ID不会设置为空。即使ID字段被标记为ID。