我有一个表单类型,其字段不在实体中作为属性,但实体有一个getter和一个与表单字段同名的setter,解释:
表格类型:
$builder->add('theField', 'entity', array(
'label' => 'The field',
'class' => 'MyAppBundle:AnEntity',
'empty_value' => '',
));
实体:
class User
{
//There is NOT a property called "theField"
public function setTheField($value)
{
...
}
public function getTheField()
{
...
}
}
所以,我希望Symfony2调用getter和setter来绑定并显示表单字段,但是我收到了这个错误:
Property theField does not exists in class My\AppBundle\Entity\User
有没有办法创建这个表单字段而不在实体中声明属性?
修改
很奇怪,但是当我声明私有财产theField
时,它是有效的(BTW,这不是我想要的)。
答案 0 :(得分:1)
你有没有尝试过:
$builder->add('theField', 'entity', array(
'label' => 'The field',
'class' => 'MyAppBundle:AnEntity',
'empty_value' => '',
'property_path' => false,
));
将您的字段名称更改为与实体中的属性相同,或将'property_path'
更改为属性名称。
$builder->add('theField', 'entity', array(
'label' => 'The field',
'class' => 'MyAppBundle:AnEntity',
'empty_value' => '',
'property_path' => 'theField',
));
在您的实体中添加:
private $theField = null;
答案 1 :(得分:0)
您也可以使用symfony的mapped
选项
$builder->add('chooseProduct', ChoiceType::class, array(
'mapped'=> false,
'required' => false,
'placeholder' => 'Choose',
'choices' => $this->entityManager->getRepository('App:Entity)->getSelectList(),
'label_attr' => array('class' => 'control-label')
));