Skip to content
Advertisement

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 :

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

    /**
     *
     * @ORMColumn(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?

Advertisement

Answer

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;
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement