Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

php - Dependency injection with custom Doctrine 2 data type

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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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 DoctrineDBALTypes::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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...