1

我用的是symfony5。我选择了easyAdminBundle。为了上传图片,我尝试使用 vichUploaderBundle。问题是它只能工作一次。例如,对于服务实体,它仅在我创建新服务(例如:seo、网站或打印)时才有效。当我更改已经存在的服务的图像时,它不再起作用。我按照官方网站上的教程...

# 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

有时它有效,但大多数时候它不...

4

1 回答 1

0
public function setImage(string $image): self

=>

public function setImage(?string $image): self

位置错误发生在二传手级别。您必须添加一个问号才能接受 null。

于 2020-01-08T11:36:10.500 回答