如何在 Symfony2 中向实体表单添加额外的非实体字段 [英] How to add additional non-entity fields to entity form in Symfony2

查看:25
本文介绍了如何在 Symfony2 中向实体表单添加额外的非实体字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用实体中的一个元素创建了表单:

I have created form with one element from Entity:

$promo = new Promo();

$form = $this->createFormBuilder($promo)
        ->add('code', 'text')
        ->getForm();

我想添加文件元素(实体中不存在此字段).当我这样做时:

And I want to add file element (this field doesn't exist in the Entity). When I do:

$form = $this->createFormBuilder($promo)
        ->add('code', 'text')
        ->add('image', 'file')
        ->getForm();

我有一个错误:属性image"和方法getImage()"都没有.如何添加此字段?

I have an error: Neither property "image" nor method "getImage()". How can I add this field?

推荐答案

使用 映射:

$form = $this->createFormBuilder($promo)
    ->add('code', 'text')
    ->add('image', 'file', array(
                "mapped" => false,
            ))
    ->getForm();

在旧的 Symfony 版本(2.0 及更早版本)中,使用 property_path:

In old Symfony versions (2.0 and earlier), use property_path:

$form = $this->createFormBuilder($promo)
    ->add('code', 'text')
    ->add('image', 'file', array(
                "property_path" => false,
            ))
    ->getForm();

"property_path" 在 Symfony 2.3 中被移除

"property_path" was removed in Symfony 2.3

这篇关于如何在 Symfony2 中向实体表单添加额外的非实体字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆