简体   繁体   English

我如何依赖注入PDO到Symfony2中的服务?

[英]How do I dependency inject PDO to a service in Symfony2?

I'm working with a library in the vendor/ directory and designating it as a service within Symonfy2. 我正在使用vendor /目录中的库并将其指定为Symonfy2中的服务。 The library has a dependency on a PDO connection -- how do I set up my service definition to pass that to the constructor? 该库依赖于PDO连接 - 如何设置我的服务定义以将其传递给构造函数?

I'm trying something like this: 我正在尝试这样的事情:

services:
    timers.mysql.service
        class : Fmp\Timers\Record
        factory_service : doctrine.orm.default_entity_manager
        factory_method : getConnection

Which is giving me errors. 这给了我错误。 Any help appreciated. 任何帮助赞赏。 Note, the library I can't modify - it needs a PDO connection, not a doctrine entity manager 注意,我无法修改的库 - 它需要PDO连接,而不是doctrine实体管理器

services:
timers.mysql.service
    class : Fmp\Timers\Record
    factory_service : database_connection
    factory_method : getWrappedConnection

That should give you an object extended from PDO. 这应该给你一个从PDO扩展的对象。

Otherwise, you might just need to define your very own pdo object. 否则,您可能只需要定义自己的pdo对象。 The database session handler used to have an example. 数据库会话处理程序曾经有一个例子。 BTW, factory_service and method are depreciated. BTW,factory_service和method是折旧的。 Might look at using the factory item. 可能会看看使用工厂项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM