我的问题与:How to configure m2m relationship in Sonata Admin?
有关根据相关答案配置管理类后我得到了:
表单的视图数据应该是标量,数组或类型 \ ArrayAccess的实例,但是是类的实例 Syloc \包\ GooglePlacesBundle \实体\ PlaceType。你可以避免这种情况 通过将“data_class”选项设置为错误 “Syloc \ Bundle \ GooglePlacesBundle \ Entity \ PlaceType”或添加视图 转换类实例的转换器 Syloc \ Bundle \ GooglePlacesBundle \ Entity \ PlaceType为标量,数组或 \ ArrayAccess的实例。
所以我添加了这个属性:
->add('types', 'collection', array(
'type' => 'text',
'allow_add' => true,
'data_class' => 'Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType',
));
这会导致错误:
表单的视图数据应该是类的实例 Syloc \ Bundle \ GooglePlacesBundle \ Entity \ PlaceType,但是是一个实例 类Doctrine \ ORM \ PersistentCollection。您可以避免此错误 通过将“data_class”选项设置为null或添加视图 转换类实例的转换器 Doctrine \ ORM \ PersistentCollection到一个实例 Syloc \捆绑\ GooglePlacesBundle \实体\ PlaceType。
答案 0 :(得分:2)
我结束了:
->add('types', 'entity', array(
'class' => 'GooglePlacesBundle:PlaceType',
'property' => 'type',
'multiple' => true,
));
答案 1 :(得分:0)
data_class
上的collection
属性不是一个选项。看起来你可能想要你的'类型':
->add('types', 'collection', array(
'type' => '\Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType',
'allow_add' = true
));
现在假设PlaceType
是您创建的自定义表单类型,而不仅仅是您的实体。
如果您正在寻找此处,请参阅我的回答,以获得进一步说明:How do I make a form with a bunch of unrelated entities in Symfony2?
答案 2 :(得分:0)
尝试使用sonata-type-collection:
->add('types', 'sonata_type_collection', array(
'by_reference' => true,
'btn_add' => false,
'required' => true,
'label' => false,
'type_options' => array('delete' => false),
'cascade_validation' => true,
), array(
'edit' => 'inline',
'inline' => 'table',
))