Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 wondering : is there a way to override the EntityManager class and specify Doctrine2 in Symfony2 to use it as implementation of the EntityManager?

Thank you for any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
623 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 DoctrineORMDecoratorEntityManagerDecorator (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(DoctrineORMEntityManager::create(

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

yourcompany_entity_manager:
    public: false
    class: YourCompanyORMEntityManagerDecorator
    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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...