Php 跳过Doctrine2中相关实体的持久化
我有一个实体“Entity1”,它与另一个实体“Entity2”有一个单向关系。Entity2由不同的EntityManager管理 以下是实体1的一部分:Php 跳过Doctrine2中相关实体的持久化,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我有一个实体“Entity1”,它与另一个实体“Entity2”有一个单向关系。Entity2由不同的EntityManager管理 以下是实体1的一部分: /** * @ORM\OneToOne(targetEntity="Entity2") * @ORM\JoinColumn(name="Entity2ID", referencedColumnName="ID") **/ protected $entity2; Entity2已存在,Entity1是一个新实体。以下是持久化逻辑的一部
/**
* @ORM\OneToOne(targetEntity="Entity2")
* @ORM\JoinColumn(name="Entity2ID", referencedColumnName="ID")
**/
protected $entity2;
Entity2已存在,Entity1是一个新实体。以下是持久化逻辑的一部分:
$obj1 = new Entity1();
$obj1->setEntity2($this->entity2Manager
->getRepository('VendorBunlde:Entity2')
->find($entity2Id));
$this->entity1Manager->persist($obj1);
$this->entity1Manager->flush();
我得到了这个错误:
A new entity was found through the relationship 'MyBundle\\Entity\\Entity1#entity2' that was not configured to cascade persist operations for entity: VendorBundle\\Entity\\Entity2@0000000008fbb41600007f3bc3681401. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={\"persist\"}). If you cannot find out which entity causes the problem implement 'VendorBundle\\Entity\\Entity2#__toString()' to get a clue
我如何才能迫使教义跳过坚持的实体2,同时保持这种关系?我尝试了“->merge($obj1)”它可以工作,但是当我调用$obj1->getId()时,我得到了null 我会尝试以下方法:
实体与EntityManager分离,因此不再受管理
通过调用EntityManager上的#detach($entity)方法或
将分离操作级联到它。对已分离文件所做的更改
实体(如有)(包括移除该实体)将不会
实体分离后已同步到数据库
但尚未尝试,请告诉我它是否有效,谢谢。此解决方案适合我:
- 我删除了entity1和entity2之间的关系
- 将@PostLoad添加到加载实体2
$obj1 = new Entity1();
$obj2 = $this->entity2Manager
->getRepository('VendorBunlde:Entity2')
->find($entity2Id);
$obj1->setEntity2($obj2);
$this->entity1Manager->detach($obj2);
$this->entity1Manager->persist($obj1);
$this->entity1Manager->flush();