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

前端 未结 4 720
你的背包
你的背包 2020-11-30 08:18

I\'m currently working with Symfony2 and Doctrine2, but I must override the Doctrine2 EntityManager and add it some \"undelete\" features (ACLs inside).

So I\'m wond

4条回答
  •  萌比男神i
    2020-11-30 08:50

    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

提交回复
热议问题