Plistfy
Plistfy

Reputation: 51

Doctrine access in SonataAdmin Class

I have a many to many association with extra fields. I resolved with a new entity and two one to many relations following this tutorial: http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html

Meeting ---> MeetingContacts <--- Contact

When I create a new meeting I need to select all the people who are going to assist, so I have a list of checkbox with all the contacts. This works perfectly in Sonata.

This is my Meeting configureFormFields class:

 ->with('Contacts', array('class' => 'col-md-6'))
            ->add('contacts' , 'entity' , array(
                      'class'    => 'AppBundle:Contact' ,
                      'expanded' => true ,
                      'multiple' => true , ))
        ->end();

My problem is that I want to filter this list checkbox in the Create Action by two properties of the Contact Entity.

The solution I founded in the forums is to pass a array to the view with the contacts for each of those values and then in twig with javascript show or hide the checkbox.

->with('Contacts', array('class' => 'col-md-6'))
            ->add('contacts' , 'entity' , array(
                      'class'    => 'AppBundle:Contact' ,
                      'expanded' => true ,
                      'multiple' => true , ), 

                       array('myArray' => myArray)))
          ->end();

Now the problem is that I can't access to the Contact Repository to create this array in the Admin Class.

I tried without success:

$em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager()

and add doctrine in the service (No Entity Manager in Custom Class and ContextErrorException)

sonata.admin.contact:
    class: AppBundle\Admin\ContactAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: "Content", label: "Contacts" }
    arguments:
        - ~
        - AppBundle\Entity\Contact
        - ~
        - @doctrine.orm.default_entity_manager

    calls:
        - [ setTranslationDomain, [AppBundle]]

but when i add the fourth argument i hace a exception for bad yml format.

How I could have access to doctrine EM in the Admin class, or how I could pass to the admin Create form this extra data?

Thanks.

Upvotes: 2

Views: 1755

Answers (1)

Plistfy
Plistfy

Reputation: 51

I found a solution:

class MeetingAdmin extends AbstractAdmin
{
    public $arrProfiles;

    protected function configureFormFields(FormMapper $formMapper)
    {

         $this->arrProfiles = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager()->getRepository('AppBundle:Contact')->findProfile();

 ...

Then in the template I can access to the array:

{{admin.arrProfiles}}

Upvotes: 3

Related Questions