Symfony VichUploaderBundle:找不到字段“ xx”的映射

时间:2018-08-08 02:21:59

标签: symfony file-upload vichuploaderbundle

我阅读了VichUploaderBundle documentation,并将从中学到的内容应用于代码。
但是,当我尝试运行代码时,出现此异常未找到字段“ logoFile”的映射
注意:我正在使用Symfony 4.1和VichUploaderBundle 1.8
这是我的代码...


配置

// config/packges/vich_uploader.yaml
vich_uploader:
    db_driver: orm
    mappings:
        company_logo:
            uri_prefix: /uploads/logos
            upload_destination: '%kernel.project_dir%/public/uploads/logos'         
            inject_on_load: false
            delete_on_update: true
            delete_on_remove: true


实体

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

/**
 * @Vich\Uploadable
 * @UniqueEntity("registrationNumber", message="This Registration Number {{ value }} is already used")
 */
class Company
{
    // ...

    /*
     * @Vich\UploadableField(mapping="company_logo", fileNameProperty="logo")
     */
    private $logoFile;

    /**
     * @ORM\Column(type="string", length=128, nullable=true, unique=true)
     */
    private $logo;

    public function getLogo(): ?string
    {
        return $this->logo;
    }

    public function setLogo(?string $logo): self
    {
        $this->logo = $logo;

        return $this;
    }

    public function setLogoFile(?File $image = null): self
    {
        $this->logoFile = $image;

        return $this;
    }

    public function getLogoFile(): ?File
    {
        return $this->logoFile;
    }

    // ...
}


表格

class CompanyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
               // ...
               ->add('logoFile', VichImageType::class);
    }
}

2 个答案:

答案 0 :(得分:1)

这里的问题显然是表单,实体很好,并且根据Vich文档(看来)。请检查有关Symfony表单用法的文档: https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html

答案 1 :(得分:0)

您的问题是您写

/*
 * @Vich\UploadableField(mapping="company_logo", fileNameProperty="logo")
 */

代替

/**
 * @Vich\UploadableField(mapping="company_logo", fileNameProperty="logo")
 */

(检查第一行;))

PS:如果您使用Easy admin,请注意表单说明,您需要定位“ logoFile”属性而不是“ logo”