问题
I am using Symfony 2.6.1.
Entities configuration: http://pastebin.com/rMkYHjkE
Admin class:
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
}
When I am trying to edit selected entity:
Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType" given
回答1:
From the docs:
This is the field type for each item in this collection (e.g. text, choice, etc). For example, if you have an array of email addresses, you'd use the email type. If you want to embed a collection of some other form, create a new instance of your form type and pass it as this option.
So you would want to do something like:
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
}
You don't define an Entity Type for a collection
Form Type. Perhaps you were also looking to do sonata_type_collection, and not just collection
? You also shouldn't need to pass the child Entity type through this Form Type as it's automatically resolved from the Entity property.
来源:https://stackoverflow.com/questions/27604150/how-to-configure-m2m-relationship-in-sonata-admin