Is there a way to specify Doctrine2 Entitymanager implementation class in Symfony2?

After Doctrine 2.4 (Doctrine 2.4 release) you need to use decorator for this. Do not extend EntityManager directly.
First you need to implement you own entity manager decorator that extends Doctrine\ORM\Decorator\EntityManagerDecorator (like @Dana)
But you can’t just change doctrine.orm.entity_manager.class to your new decorator because EntityManagerDecorator requires EntityManagerInterface in it’s constructor:

public function __construct(EntityManagerInterface $wrapped)

You can’t just pass doctrine.orm.entity_manager as a parameter here because it will be a recursion.
And don’t do like this:

return new self(\Doctrine\ORM\EntityManager::create(

What you need is to configure your decorator in services like a decorator:

yourcompany_entity_manager:
    public: false
    class: YourCompany\ORM\EntityManagerDecorator
    decorates: doctrine.orm.default_entity_manager
    arguments: ["@yourcompany_entity_manager.inner"]

Now you’ll have your decorator as a default entity manager for Doctrine. @yourcompany_entity_manager.inner is actually a link to doctrine.orm.default_entity_manager that will be passed to yourcompany_entity_manager constructor.

Symfony docs for configuring decorators: link

Btw this command is very useful to debug your services:

app/console container:debug | grep entity_manager

Leave a Comment