Search code examples
phpsymfonysymfony-3.4

How to configure Doctrine Entity Manager as a public Symfony service?


I'd like to configure Doctrine Entity Manager as a public Symfony service.

I tried adding the following configuration blocks in services.yml (one at a time):

Doctrine\ORM\EntityManagerInterface:                                                                                
    public: true            
# [OUTPUT] Error: Cannot instantiate interface Doctrine\ORM\EntityManagerInterface                                                                       

Doctrine\ORM\EntityManager:                                                                                         
    public: true                                                                                                    
# [OUTPUT] Invalid service "Doctrine\ORM\EntityManager": its constructor must be public.

doctrine.orm.default_entity_manager:                                                                                
    public: true  
# [OUTPUT] The definition for "doctrine.orm.default_entity_manager" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.

Judging by the output error messages, I suspect that the definitions are incomplete, but I didn't find anywhere the correct way to manually configure the Doctrine Entity Manager service.


Solution

  • As per Cerad's comment, I had to add alias: 'doctrine.orm.default_entity_manager' to my first attempted configuration.

    So the correct solution is:

    Doctrine\ORM\EntityManagerInterface:                                                                                
        public: true
        alias: 'doctrine.orm.default_entity_manager'