Dependency injection with custom Doctrine 2 data type

六眼飞鱼酱① 提交于 2021-02-15 06:15:54

问题


I have pretty much an identical issue as Dependency injection with custom Doctrine 2 hydrator, but I need to inject a service into a custom data type, not into a hydrator.

The solution in the referenced question is relies on duplicating and modifying doctrine source code as Doctrine initializes the classes itself. Hopefully another approach is viable to custom data types?

This is for a Symfony3 application, if there could be some magic to be applied there.


回答1:


Per the comments in the initial question:

What makes this hard is how Doctrine instantiates custom data types. The Type::addType($name, $className) method for doctrine just wants the class name, not an instance of a class. What you could do is add your custom doctrine type within your bundles extension class, then register an event listener service on on kernel.request that has the needed dependencies and set them into your data type by calling \Doctrine\DBAL\Types::getType($name)->setSomeDependency($dependency);. Kinda messy though.

Good find on Matthias' article. The boot() method of the extension class does look like a more natural place to set the dependencies.



来源:https://stackoverflow.com/questions/38230885/dependency-injection-with-custom-doctrine-2-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!