Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Doctrine2 entity listener as service in Symfony2

For the context, I use Symfony 2.4 & Doctrine 2.4.

I'm currently trying to move some logic inside doctrine entity listeners. For now, I have just used the @EntityListeners annotation in order to register my listeners on my entities as it does not need external dependencies. But now, I would like to inject services into my entity listeners though the Symfony2 container. As this feature is quite new, there is not related doc explaining if it is first possible & second, how it can be done.

Thanks!

like image 486
egeloen Avatar asked Oct 23 '13 22:10

egeloen


1 Answers

This is possible in Doctrine 2.4 but requires you to create your own Entity Listener Resolver class.

See Doctrine docs for details on registering a custom resolver which takes a container (or ServiceManager in my case) as a constructor param.

I have not tried this with the Symfony2 container but I have with Zend Framework 2's ServiceManager module and the principles are likely to be extremely similar.

ZF2's DoctrineORMModule makes this whole process very elegant by using the ServiceManager to call MyEntityListenerResolverFactory which in turn injects the ServiceManager into the MyEntityListenerResolver.

like image 133
alexb Avatar answered Oct 03 '22 08:10

alexb