Php 如何在Symfony2中向实体窗体添加其他非实体字段
我已使用实体中的一个元素创建了表单:Php 如何在Symfony2中向实体窗体添加其他非实体字段,php,symfony,Php,Symfony,我已使用实体中的一个元素创建了表单: $promo = new Promo(); $form = $this->createFormBuilder($promo) ->add('code', 'text') ->getForm(); 我想添加file元素(该字段在实体中不存在)。当我这样做时: $form = $this->createFormBuilder($promo) ->add('code', 'text'
$promo = new Promo();
$form = $this->createFormBuilder($promo)
->add('code', 'text')
->getForm();
我想添加file元素(该字段在实体中不存在)。当我这样做时:
$form = $this->createFormBuilder($promo)
->add('code', 'text')
->add('image', 'file')
->getForm();
我有一个错误:属性“image”和方法“getImage()”都没有。如何添加此字段?使用以下选项:
$builder->add('image'、'file'[
“属性路径”=>false,
]);
使用:
在旧Symfony版本(2.0及更早版本)中,使用:
Symfony 2.3中删除了“属性路径”。不知道映射的
选项。谢谢。我将使用属性_path=false。它对SF4也很有帮助。自Symfony 2.3以来,该属性已被删除。新方法是在新的正确答案中使用mapped
like。
$form = $this->createFormBuilder($promo)
->add('code', 'text')
->add('image', 'file', array(
"mapped" => false,
))
->getForm();
$form = $this->createFormBuilder($promo)
->add('code', 'text')
->add('image', 'file', array(
"property_path" => false,
))
->getForm();