使用虚拟 属性 提交表单

submit form with a virtual property

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

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

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

根据 and the documentation,我需要将 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;
    }

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

Could not determine access type for property "imageToFill" in class "App\Entity\FieldTemplate": Neither the property "imageToFill" nor one of the methods "addImageToFill()"/"removeImageToFill()", "setImageToFill()", "imageToFill()", "__set()" or "__call()" exist and have public access in class "App\Entity\FieldTemplate".

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

不容易。处理表单时,它会向 setImageToFill() 属性.

even if the imageToFill property is a virtual property and doesn't send anything to the db

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

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

它会非常有效