phpsymfonyvichuploaderbundle

why cannot I delete my image entity using VichUploader?


I work with Symfony 4 and Doctrine And i'm using VichUploader to manage my images.

I create an entity image and when I use this entity to add new images it works like a charm but when I want to delete it with my controller :

    public function delete(Image $image):Response
    {
        $em = $this->getDoctrine()->getEntityManager();
        $em->remove($image);
        $em->flush();

        return new RedirectResponse($this->generateUrl('image-index'));
    }

I get the strangest error :

Expected argument of type "string", "NULL" given at property path "fileName".

with `filename being as such in my Image entity :

     /** 
     * @Vich\UploadableField(mapping="picture", fileNameProperty="fileName")
     * @var File
     */
    private $imageFile;

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

The same behavior appear when dealing with editing the entity. Do you see anything that I did wrong?


Solution

  • It looks like you're using a type hinting on your setter, something like

    pubilc function setFileName(string $name): void
    {
        $this->fileName = $name;
    }
    

    You need to use a less strict type, like this:

    pubilc function setFileName(?string $name): void
    {
        $this->fileName = $name;
    }