Search code examples
vichuploaderbundleeasyadminsymfony5

Symfony5 easyAdmin - vichUploader


I'm with symfony5. I opted for the easyAdminBundle. In order to upload the images, I try to use vichUploaderBundle. The problem is that it only works once. For example for the Service entity, it only works when I create a new service (ex: seo, website or print). When I change the image of a service that already exists, it no longer works. I followed the tutorial on the official site ...

# config/services.yaml
parameters:
    app.path.website_images: /images


# config/packages/vich_uploader.yaml
vich_uploader:
    db_driver: orm
    mappings:
        website_images:
            uri_prefix:         '%app.path.website_images%'
            upload_destination: '%kernel.project_dir%/public%app.path.website_images%'


?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ServiceRepository")
 * @Vich\Uploadable
 */
class Service
{
    [...]

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $image;

    /**
     * @Vich\UploadableField(mapping="website_images", fileNameProperty="image")
     */
    private $imageFile;


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

  [...]

}


# config/packages/easy_admin.yaml
Service:
    label:           'Service'
    class:           App\Entity\Service
    list:
        fields:
            - { property: 'title' }
            - { property: 'image', type: 'image', base_path: '%app.path.website_images%' }
    form:
        fields:
            - { property: 'title' }
            - { property: 'imageFile', type: 'vich_image' }
            - { property: 'content', type: 'fos_ckeditor' }
  • Symfony 5.02
  • EasyAdminBundle 2.x

Sometimes it works, but most of the time it doesn't ...


Solution

  • public function setImage(string $image): self
    
    =>
    
    public function setImage(?string $image): self
    

    The location error was at the setter level. You must add a question mark to accept null.