我有一个与图像实体"一对多"关联的新闻实体:
class Noticia
{
function __construct()
{
$this->images = new ArrayCollection();
}
/**
* @ORMOneToMany(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实体中删除,但该实体未删除
你能告诉我如何解决这个问题吗?
PS:在NoticiaAdmin我有:
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,
]
);
}
}
您可以尝试使用此注释来指定需要删除该noticia id引用的列:
/**
* @ORMOneToMany(targetEntity="ImagemNoticia", mappedBy="noticia", cascade={"all"}, orphanRemoval=true)
* @ORMJoinColumn(name="noticia_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $images;
我自己解决了。
我禁用APC原则metadata_cache_driver并运行"控制台原则:缓存:清除元数据"