如何在不使用合并的情况下更新/替换 Doctrine 中的现有对象/实体?

And*_*ord 6 doctrine symfony doctrine-orm

目前我正在Doctrine 2一个Symfony 2.8项目中工作。在线数据可以与移动应用程序同步。当在线项目从移动应用程序接收数据时,接收到的实体将从 JSON 反序列化并merged放入在线数据库中。使用$entityManger->merge($receivedEntity)自动确保插入新实体并更新现有实体。

EntityManager::merge将不再支持此工作正常Doctrine 3

为此,我想手动处理合并:

public function mergeEntity($receivedEntity, $class) {
   $repo = $this->em->getRepository($class);
   $existingEntity = $repo->findOneById($receivedEntity->getId());

   if (!$existingEntity) {
       // Insert NEW entity
       $this->em->persist($receivedEntity);
       $this->em->flush();
   } else {
       // Update EXISTING entity
       $existingEntity->setProperty1($receivedEntity->getProperty1());
       $existingEntity->setProperty2($receivedEntity->getProperty2());
       ...
       $existingEntity->setPropertyN($receivedEntity->getPropertyN());

       $this->em->persist($existingEntity);
       $this->em->flush();
   }
}
Run Code Online (Sandbox Code Playgroud)

虽然这项工作很繁琐,而且不是很灵活。每次实体类更改时,例如添加、删除或更新属性,合并方法也必须更新。此外,该方法仅适用于一个特定的类,每个实体类都需要自己的合并方法。

虽然反射可以用来限制这些问题,但它仍然比现有的复杂得多EntityManager::merge......

难道不能以某种方式用新的“版本”替换现有实体吗?

// Is something like this possible/available?
$this->em->replaceEntity($receivedEntity);
Run Code Online (Sandbox Code Playgroud)

简而言之:创建完全自定义的更新/替换方法是更新现有实体的正确方法吗?或者是否有任何内置功能(旁边merge)可用于实现这一目标?