Symfony5 easyAdmin - vichUploader

Symfony5 easyAdmin - vichUploader

我在使用 symfony5。我选择了 easyAdminBundle。为了上传图片,我尝试使用vichUploaderBundle。问题是它只能工作一次。例如,对于服务实体,它仅在我创建新服务(例如:seo、网站或打印)时有效。当我更改已存在的服务的图像时,它不再有效。我关注了 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' }

有时有效,但大多数时候无效...

public function setImage(string $image): self

=>

public function setImage(?string $image): self

位置错误在 setter 级别。您必须添加一个问号才能接受 null。