2013-03-13 81 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