Sonata Admin sonata_type_collection不删除子实体

时间:2015-04-09 23:52:03

标签: symfony one-to-many sonata-admin

我有一个与图像实体“一对多”关联的新闻实体:

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实体中删除,但不删除此实体

你能告诉我如何解决这个问题吗?

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,
                ]
            );
    } 
}

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此注释,指定需要删除使用该noticia id引用的列:

/**
 * @ORM\OneToMany(targetEntity="ImagemNoticia", mappedBy="noticia", cascade={"all"}, orphanRemoval=true)
 * @ORM\JoinColumn(name="noticia_id", referencedColumnName="id", onDelete="CASCADE")
 */

private $images;

答案 1 :(得分:0)

我解决了自己。

我禁用APC doctrine metadata_cache_driver并运行“console doctrine:cache:clear-metadata”