我有一个城市添加表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', null, array(
'label' => 'City',
));
$builder->add('country', 'genemu_jqueryautocompleter_entity', array(
'class' => 'X\tBundle\Entity\Country',
'property' => 'name',
'route_name' => 'ajax_country',
'required' => true,
'label' => 'Country',
));
}
国家是一个实体 . 当我提交此表单时 - S2返回错误: cannot save because cannot convert Object to String
为此,我使用魔法 __toString()
并返回 (string)$this->getId()
;
但我不确定 - 这是对的吗?
2 回答
最好的办法是为您的国家/地区实体创建一个FormType并将其嵌入主窗体中 . 请参阅Cookbook中的How to Embed a Collection of Forms来解释如何执行此操作 .
你想要做的事情是可能的,但它会使事情变得非常混乱 .
答案:Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting
所以,我们使用下一件事: