简体   繁体   中英

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM