2013-03-13 82 views
-1

保存Symfony2的实体形式我有一个城市添加形式:与实体领域

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();

但我不确定 - 这是正确的吗?

Desicion:Symfony/Doctrine: "Catchable Fatal Error: Object of class <type> could not be converted to string" when persisting

+1

,你能告诉我们你的实体?你的桌子之间有链接吗? – Snroki 2013-03-13 12:10:23

+0

而不是在标题内写*解*,你可以发布答案并接受它吗? – j0k 2013-03-13 14:02:19

+0

@ j0k,我不能在帖子后的6个帖子中回复我的问题,所以我在标题中写了Solved。并将决定链接设置为内容。 – Salavat 2013-03-14 07:23:33

回答

1

最好的事情是创建一个FormType在您的国家实体,其嵌入的主要形式。请参阅Cookbook的How to Embed a Collection of Forms以准确解释如何执行此操作。

你想要做的事情是可能的,但它会让事情变得非常迅速。

+0

我需要自动完成。 – Salavat 2013-03-13 12:59:50