Symfony Sonata管理Sonata_类型_集合不删除子实体
我有一个与图像实体“一对多”关联的新闻实体:Symfony Sonata管理Sonata_类型_集合不删除子实体,symfony,one-to-many,sonata-admin,Symfony,One To Many,Sonata Admin,我有一个与图像实体“一对多”关联的新闻实体: class Noticia { function __construct() { $this->images = new ArrayCollection(); } /** * @ORM\OneToMany(targetEntity="ImagemNoticia", mappedBy="noticia", cascade={"all"}, orphanRemoval=true)
class Noticia
{
function __construct()
{
$this->images = new ArrayCollection();
}
/**
* @ORM\OneToMany(targetEntity="ImagemNoticia", mappedBy="noticia", cascade={"all"}, orphanRemoval=true)
*/
private $images;
public function removeImage(ImagemNoticia $imagem) {
if ($this->images->contains($imagem) {
$this->images->removeElement($imagem);
}
$imagem->setNoticia(null);
return $this;
}
}
和一个管理类:
class NoticiaAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('images', 'sonata_type_collection',
array(
'by_reference' => false,
'required' => false,
), array(
'edit' => 'inline'
'inline' => 'table'
)
);
}
}
插入和更新图像效果很好
当我在嵌入表单的图像中标记“删除”并保存时,实体关联字段将从ImagemNoticia实体中删除,但此实体不会删除
你能告诉我怎么解决这个问题吗
附言:
在Notifiaadmin中,我有:
public function preUpdate($object)
{
foreach ($object->getImagens() as $imagem) {
$imagem->setNoticia($object);
}
parent::preUpdate($object);
}
ImagemNoticiaAdmin是:
class ImagemNoticiaAdmin extends Admin {
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$imagemNoticia = $this->getSubject();
if ($imagemNoticia) {
$formMapper->add(
'file',
'liip_imagine_image',
[
'image_path' => $imagemNoticia->getWebPath(),
'image_filter' => 'noticia_thumb',
]
);
} else {
$formMapper->add(
'file',
'file'
);
}
$formMapper
->add(
'flag',
'choice',
[
'choices' => ImagemNoticia::getFlags(),
'label' => 'Tipo'
]
)
->add(
'legenda',
null,
[
'required' => false,
]
);
}
}
您可以尝试使用此注释,该注释指定需要删除使用该Notifia id引用的列:
/**
* @ORM\OneToMany(targetEntity="ImagemNoticia", mappedBy="noticia", cascade={"all"}, orphanRemoval=true)
* @ORM\JoinColumn(name="noticia_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $images;
我自己解决了
我禁用APC条令元数据\u缓存\u驱动程序并运行“控制台条令:缓存:清除元数据”您是否有ImagemNoticia的管理类?如果有,请复制粘贴。试着使用preUpdate()。2我忘了提到:是的,我有ImagemNoticia的管理类,并且已经使用了preUpdate@maches:我编辑了我的问题,以包括我所知的预更新e ImagemNoticiaAdmin类
onDelete=“CASCADE”
用于关系的“子”端(多对一)并删除所有ImageNoticia实体,如果我删除与这些实体相关的Noticia实体,这不是您想要的吗?删除时删除ImageNotifia与Notifia实体的关系?真的吗?因为我认为在admin'by_reference'=>false中,这意味着数据库中的引用没有被考虑在内。