使用VichUploader的Symfony BootstrapCollection - 元素删除不起作用

时间:2016-12-16 10:18:51

标签: twitter-bootstrap entity-framework symfony doctrine vichuploaderbundle

我正在使用BootstrapCollection http://bootstrap.braincrafted.com/ 使用VichUploader https://github.com/dustin10/VichUploaderBundle

除删除功能外,它工作正常。 如果我尝试删除子项(PDF文件),则只有name属性为空,但不会删除整个行/实体。

  

父formType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'files',
            BootstrapCollectionType::class,
            [
                'type'               => NewsPdfFileType::class,
                'required'           => false,
                'allow_add'          => true,
                'allow_delete'       => true,
                'label'              => 'PDF',
                'translation_domain' => 'entities',
            ]
        );
    }
  

Child FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'pdfFile',
                VichFileType::class,
                [
                    'required'      => false,
                    'download_link' => true,
                    'allow_delete'  => true,
                    'label'         => 'PDF'
                ]
            );

    }

这是我的关系:

  

父实体NewsPdf:

class NewsPdf
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Brightside\CMSBundle\Entity\News")
     */
    private $news;

    /**
     * @ORM\OneToMany(targetEntity="Mandant\CMSPlusBundle\Entity\NewsPdfFile", mappedBy="newsPdf", cascade={"persist"})
     */
    private $files;
.
.
.
.
  

儿童实体NewsPdfFile:

class NewsPdfFile
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Mandant\CMSPlusBundle\Entity\NewsPdf", inversedBy="files")
     * @JoinColumn(name="newsPdf_id", referencedColumnName="id")
     */
    private $newsPdf;

    /**
     * @var File
     *
     * @Vich\UploadableField(mapping="news_pdf", fileNameProperty="pdfName")
     *
     */
    private $pdfFile;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     *
     * @var string
     */
    private $pdfName;

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    private $updatedAt;
.
.
.
  

在我的父实体中,我已经有了删除功能:

/**
     * @param NewsPdfFile $file
     */
    public function removeFile(NewsPdfFile $file)
    {
        $this->files->removeElement($file);
    }

但是我无法让它发挥作用。 当我尝试删除元素时(在我的formType中有allow_delete选项),只有“pdfName”属性为空。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

我自己找到了灵魂:

Symfony does not remove entity from collection

我需要在父实体中更改我的删除功能:

    /**
     * @param NewsPdfFile $file
     */
    public function removeFile(NewsPdfFile $file)
    {
        $this->files->removeElement($file);
        $file->setPdfFile(null);
    }
在OneToMany注释中

和orphanRemoval = true:

    /**
     * @ORM\OneToMany(targetEntity="Mandant\CMSPlusBundle\Entity\NewsPdfFile", mappedBy="newsPdf", cascade={"persist"}, orphanRemoval=true)
     */
    private $files;