Nic*_*eau 11 php doctrine-orm zend-framework2
我有一个拥有实体,与"属性"实体有以下关系:
/**
* @ORM\OneToMany(targetEntity="Attribute", mappedBy="entity", cascade={"persist", "remove", "merge"})
**/
protected $attributes;
Run Code Online (Sandbox Code Playgroud)
另一方面,拥有的实体关系如下所示:
/**
* @ORM\ManyToOne(targetEntity="Entity", inversedBy="attributes")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id")
*/
protected $entity;
Run Code Online (Sandbox Code Playgroud)
当我创建实体的实例时,向其添加属性并保存它.一切正常.当我从实体中删除一个属性并保持不变时,该属性不会在数据库中删除,并在刷新时重新显示.
有人有想法吗?
rad*_*nan 33
您正在寻找的是孤儿清除.
如果您想了解当前情况不起作用的详细信息,您可以继续阅读.
不幸的是,级联操作不会做你想要的."cascade = [remove]"只是意味着如果删除了实体对象,那么doctrine将遍历并删除所有子属性:
$em->remove($entity);
// doctrine will basically do the following automatically
foreach ($entity->getAttributes() as $attr)
{
$em->remove($attr);
}
Run Code Online (Sandbox Code Playgroud)
如果您需要从实体中删除属性,则删除该属性,如下所示:
$entity->getAttributes()->removeElement($attr);
$em->remove($attribute);
Run Code Online (Sandbox Code Playgroud)
但是,要自动执行此操作,我们使用孤立删除选项.我们只是告诉学说属性只能属于实体,如果属性不再属于实体,只需删除它:
/**
* @ORM\OneToMany(targetEntity="Attribute", mappedBy="entity", orphanRemoval=true, cascade={"persist", "remove", "merge"})
**/
protected $attributes;
Run Code Online (Sandbox Code Playgroud)
然后,您只需执行以下操作即可删除该属性:
$entity->getAttributes()->removeElement($attr);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14833 次 |
| 最近记录: |