使用多个DBAL连接时自动连接特定的DBAL连接 [英] Autowire specific DBAL connection when using multiple of them

查看:119
本文介绍了使用多个DBAL连接时自动连接特定的DBAL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Doctrine 2,其中有多个DBAL连接。我在ORM中也有多个EntityManager。

I'm using Doctrine 2 where I have multiple connections for DBAL. I have also multiple EntityManagers in ORM.

我需要能够以某种方式将特定的DBAL连接自动连线到其他Symfony 3服务。

I need to be able to somehow autowire specific DBAL connection into other Symfony 3 services.

我可以自动连线使用EntityManagerDecorator的任何EntitiyManager,但不知道如何对连接执行相同操作。我可以从EntityManager中获得连接,但是我认为这不是必须的。

I can autowire any EntitiyManager using EntityManagerDecorator but don't know how to do the same with connection. I'm able to get the connection from EntityManager but I don't think it's the way to go.

推荐答案

您可以指定用于学说连接的包装器类,不需要代理类:

You can specify wrapper class for doctrine connections, no proxy class needed:

#config.yml
doctrine:
    dbal:
        connections:
            default:
                wrapper_class: AppBundle\Connections\ConnectionDefault
                ...
            second:
                wrapper_class: AppBundle\Connections\ConnectionSecond
                ...

连接应扩展 Doctrine\ DBAL\Connection

<?php

namespace AppBundle\Connection;

use Doctrine\DBAL\Connection;

class ConnectionDefault extends Connection
{

}

class ConnectionSecond extends Connection
{

}

并创建服务别名:

#services.yml
services:
    ...
    AppBundle\Connections\ConnectionDefault: '@doctrine.dbal.default_connection'
    AppBundle\Connections\ConnectionSecond: '@doctrine.dbal.second_connection'

然后,您可以简单地

class MyService {
    public function __construct(ConnectionDefault $connection) {...}
}

class MyOtherService {
    public function __construct(ConnectionSecond $connection) {...}
}

这篇关于使用多个DBAL连接时自动连接特定的DBAL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆