How to configure m2m relationship in Sonata Admin?

两盒软妹~` 提交于 2019-12-12 03:19:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!