1

我目前正在使用 Symfony 4 中 2.3 版本的 easy-admin 包。

我尝试为新视图创建一个虚拟属性。我有以下配置:

easy-admin:
    entities:
        FieldTemplate:
            class: App\Entity\FieldTemplate                  
            edit: 
                fields:
                    - { property: imageToFill, type_options: { block_prefix: 'field_to_fill'} }

根据https://stackoverflow.com/a/58710631/6734243文档,我需要将 setter 和 getter 添加到我所做的虚拟实体中:

//src/Entity/FieldTemplate.php

/**
 * @ORM\Entity(repositoryClass="App\Repository\FieldTemplateRepository")
 */
class FieldTemplate
{
    //virtual functions to display the image of the fieldTemplate in easyadminbundle

    public function getImageToFill()
    {
        return $this->getImage();
    }

    public function setImageToFill()
    {
        //do nothing
        return $this;
    }

该字段显示正确,但是当我保存时出现以下错误,这表明我的虚拟属性没有设置器。

无法确定类“App\Entity\FieldTemplate”中属性“imageToFill”的访问类型:属性“imageToFill”和方法之一“addImageToFill()”/“removeImageToFill()”、“setImageToFill()”、“ imageToFill()"、"__set()" 或 "__call()" 存在并且在类 "App\Entity\FieldTemplate" 中具有公共访问权限。

这种行为是错误还是我误解了什么?

4

1 回答 1

2

不是一件容易的事。处理表单时,它会向 setImageToFill()属性发送一些内容。

即使该imageToFill属性是虚拟属性并且不向数据库发送任何内容

因此,即使它什么都不做,也可以为您添加一个虚拟参数,setter例如

setImageToFill($dummy){
    // do nothing 
    return $this
}

它会像魅力一样发挥作用

于 2019-11-27T11:07:43.420 回答