2014-12-22 59 views
0

我正在使用Symfony 2.6.1。如何在Sonata Admin中配置m2m關係?

實體配置:http://pastebin.com/rMkYHjkE

管理類:

class PlaceAdmin extends Admin 
{ 
    // Fields to be shown on create/edit forms 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      //other fields 
      ->add('types', 'collection', array(
       'type'   => new PlaceType, 
       'allow_add' => true, 
      )); 
     ; 
    } 
    //other stuff 
} 

當我試圖編輯所選實體:

預期類型「的字符串參數, 的Symfony \分量\表\ ResolvedFormTypeInterface或 Symfony \ Component \ Form \ FormTypeInterface「, 」Syloc \ Bundle \ GooglePlacesBu ndle \實體\ PlaceType」給定

回答

1

the docs

這是該集合中的每個項目中的字段類型(例如文本,選擇等)。例如,如果您有一組電子郵件地址,則可以使用電子郵件類型。如果要嵌入其他表單的集合,請創建一個新的表單類型實例並將其作爲此選項傳遞。

所以,你會想要做的事,如:

class PlaceAdmin extends Admin 
{ 
    // Fields to be shown on create/edit forms 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      //other fields 
      ->add('types', 'collection', array(
       'type'   => 'text', 
       'allow_add' => true, 
      )); 
     ; 
    } 
    //other stuff 
} 

您沒有爲collection表單類型定義的實體類型。也許你也在尋找sonata_type_collection,而不僅僅是collection?您也不需要通過此表單類型傳遞子實體類型,因爲它自動從實體屬性解析。