跳过 Doctrine2 中相关实体的持久化
Skip persisting of a related entity in Doctrine2
我有一个实体 "Entity1",它与另一个实体 "Entity2" 具有 OneToOne(单向)关系。 Entity2 由不同的 EntityManager 管理。
这是 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
如何强制 Doctrine 跳过持久化 Entity2 并同时保持关系?我试过 "->merge($obj1)" 它有效,但当我调用 $obj1->getId() 时我得到空值?!
我会尝试 detach 方法:
An entity is detached from an EntityManager and thus no longer managed
by invoking the EntityManager#detach($entity) method on it or by
cascading the detach operation to it. Changes made to the detached
entity, if any (including removal of the entity), will not be
synchronized to the database after the entity has been detached.
$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();
还没试过,如果有效请告诉我,谢谢。
这个解决方案对我有用:Using Relationships with Multiple Entity Managers
- 我删除了 entity1 和 entity2 之间的关系。
- 添加了@PostLoad 以加载实体 2。
我有一个实体 "Entity1",它与另一个实体 "Entity2" 具有 OneToOne(单向)关系。 Entity2 由不同的 EntityManager 管理。
这是 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
如何强制 Doctrine 跳过持久化 Entity2 并同时保持关系?我试过 "->merge($obj1)" 它有效,但当我调用 $obj1->getId() 时我得到空值?!
我会尝试 detach 方法:
An entity is detached from an EntityManager and thus no longer managed by invoking the EntityManager#detach($entity) method on it or by cascading the detach operation to it. Changes made to the detached entity, if any (including removal of the entity), will not be synchronized to the database after the entity has been detached.
$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();
还没试过,如果有效请告诉我,谢谢。
这个解决方案对我有用:Using Relationships with Multiple Entity Managers
- 我删除了 entity1 和 entity2 之间的关系。
- 添加了@PostLoad 以加载实体 2。