Symfony 4 Easy Admin和Vich Uploader捆绑错误

时间:2018-12-18 15:34:29

标签: php symfony4 vichuploaderbundle easyadmin

我正在使用带有symfony4的easy-admin的vich-uploader捆绑包。 设置完成后,尝试删除或更新实体时出现此错误

  

在属性路径“ imageName”中给出的类型为“ string”,“ NULL”的预期参数。

在这种常见情况下,应用程序的配置最容易。 地雷测试实体:

<?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\TestRepository")
 * @Vich\Uploadable
 */
class Test
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

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

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

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


public function getId()
{
    return $this->id;
}

public function getImageName()
{
    return $this->imageName;
}

public function setImageName(string $image)
{
    $this->imageName = $image;

    return $this;
}

public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if ($image) {
        $this->updatedAt = new \DateTime('now');
    }
}

public function getImageFile()
{
    return $this->imageFile;
}


public function getUpdatedAt()
{
    return $this->updatedAt;
}

public function setUpdatedAt(\DateTime $datetime)
{
    $this->updatedAt = $datetime;

    return $this;
}
}

易于管理的Yaml配置

easy_admin:
    entities:
        Test:
            class: App\Entity\Test
            form:
                fields:
                    - { property: 'imageFile', type: 'vich_image' }
            # ...
            list:
                fields:
                    - { property: 'image', type: 'image', base_path: '%app.path.banner_images%' }
            # ...
            show:
                fields:
                    - { property: 'image', type: 'image', base_path: '%app.path.banner_images%' }

1 个答案:

答案 0 :(得分:3)

尝试

public function setImageName(?string $image)
{
    $this->imageName = $image;

    return $this;
}