以下是3个实体:
实体A
class EntityA
{
/**
* @ORM\OneToMany(targetEntity="EntityB", mappedBy="entityA", cascade={"all"}, orphanRemoval=true)
*/
protected $entitiesB;
public function __construct()
{
$this->entitiesB = new ArrayCollection();
}
public function getEntitiesB()
{
return $this->entitiesB;
}
public function setEntitiesB($entitiesB)
{
$this->entitiesB = new ArrayCollection();
return $this->addEntitiesB($entitiesB);
}
public function addEntityB(EntityB $entityB)
{
if (!$this->entitiesB->contains($entityB))
{
$this->entitiesB->add($entityB);
$entityB->setEntityA($this);
}
return $this;
}
public function addEntitiesB($entitiesB)
{
foreach ($entitiesB as $entityB)
{
$this->addEntityB($entityB);
}
return $this;
}
public function removeEntityB(EntityB $entityB)
{
if ($this->entitiesB->contains($entityB))
{
$this->entitiesB->removeElement($entityB);
}
return $this;
}
public function removeEntitiesB($entitiesB)
{
foreach ($entitiesB as $entityB)
{
$this->removeEntityB($entityB);
}
return $this;
}
}
实体B
class EntityB
{
/**
*
* @ORM\OneToMany(targetEntity="EntityC", mappedBy="entityB", cascade={"all"}, orphanRemoval=true)
*/
protected $entitiesC;
/**
* @ORM\ManyToOne(targetEntity="EntityA", inversedBy="entitiesB")
* @ORM\JoinColumn(name="entity_a_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $entityA;
public function __construct()
{
$this->entitiesC = new ArrayCollection();
}
public function getEntityA()
{
return $this->entityA;
}
public function setEntityA(EntityA $entityA)
{
$this->entityA = $entityA;
$entityA->addEntityB($this);
return $this;
}
public function getEntitiesC()
{
return $this->entitiesC;
}
public function setEntitiesC($entitiesC)
{
$this->entitiesC = new ArrayCollection();
return $this->addEntitiesC($entitiesC);
}
public function addEntityC(EntityC $entityC)
{
if (!$this->entitiesC->contains($entityC))
{
$this->entitiesC->add($entityC);
$entityC->setEntityB($this);
}
return $this;
}
public function addEntitiesC($entitiesC)
{
foreach ($entitiesC as $entityC)
{
$this->addEntityC($entityC);
}
return $this;
}
public function removeEntityC(EntityC $entityC)
{
if ($this->entitiesC->contains($entityC))
{
$this->entitiesC->removeElement($entityC);
}
return $this;
}
public function removeEntitiesC($entitiesC)
{
foreach ($entitiesC as $entityC)
{
$this->removeEntityC($entityC);
}
return $this;
}
}
实体C
class EntityC
{
/**
* @ORM\ManyToOne(targetEntity="EntityB", inversedBy="entitiesC")
* @ORM\JoinColumn(name="entity_b_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $entityB;
public function getEntityB()
{
return $this->entityB;
}
public function setEntityB(EntityB $entityB)
{
$this->entityB = $entityB;
$entityB->addEntityC($this);
return $this;
}
}
所以,现在,假设我们有这些数据:
EntityA [
EntitiesB [
EntityB1 [
EntitiesC [
EntityC1
EntityC2
EntityC3
]
]
EntityB2 [
EntitiesC [
EntityC4
]
]
]
]
我想要的是将EntityC4
转移到EntityB1[EntitieC] collection
。
要实现这一目标,过程将是:
- EntityB2.EntitiesC::removeEntityC(EntityC4)
- EntityB1.EntitiesC::addEntity(EntityC4)
但它不会起作用...... EntityC4
被移除而不被转移!
因此,它在orphanRemoval=false
上EntityB.EntitiesC
时有效,但我想保留此主义标志。
这是另一种方法吗?
感谢您的想法。
答案 0 :(得分:1)
如果您阅读了Doctrine文档chapter 8.7. Orphan Removal,您可以看到以下内容:
使用
orphanRemoval=true
选项时,Doctrine会假设实体是私有的,并且 NOT 可以被其他实体重用。如果您忽略了这个假设,即使您将孤立实体分配给另一个实体,您的实体也会被Doctrine删除。
在我看来,这正是你在这种情况下犯的错误。
在这种情况下,您根本无法使用orphanRemoval
。