Search code examples
symfonyentitysonata-adminentitymanager

Symfony: multiple entity manager (sonata)


I have a hard problem that wants an answear. I am working with Symfony and I installed Sonata to manage the admin area. After I completed to do that, my prompt line give me this error:

This is the error

This is my code:

parameters:

services:       
    app.security.user_login_form_authenticator:
        class: AppBundle\Security\UserLoginFormAuthenticator
        autowire: true

    app.security.admin_login_form_authenticator:
        class: AppBundle\Security\AdminLoginFormAuthenticator
        autowire: true

Please, help me.


Solution

  • Autowiring feature is handy, but it has a limitations.

    As you say, you have multiple instances of entity manager. So, Symfony doesn't know which of them should be injected into your services. If a service definition were available to change, you would set the autowiring_types parameter to specify a default implementation of dependency. But usualy entity manager services are defined by DoctrineBundle and you can not configure it directly. (As I know, Doctrine configuration doesn't provide options to set up that.)

    So, the easiest way is to manually specify the entity manager: just pass a entity manager service ID (doctrine.orm.XXX_entity_manager) to constructor arguments of your services.

    services:       
        app.security.user_login_form_authenticator:
            class: AppBundle\Security\UserLoginFormAuthenticator
            arguments: [ '@doctrine.orm.XXX_entity_manager' ]
    
        app.security.admin_login_form_authenticator:
            class: AppBundle\Security\AdminLoginFormAuthenticator
            arguments: [ '@doctrine.orm.YYY_entity_manager' ] 
    

    Obviously, if services have other dependendecies, you also need to specify them.