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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With