Symfony 3.4 和 Doctrine:如何使用连接名称从服务内部获取 config.yml 中定义的连接

Symfony 3.4 and Doctrine: How can I get the connections defined in config.yml from inside a service using the name of the connection

我有两个数据库,一个用于生产,一个用于备份。我想在 Symfony 中有一个服务,从一个命令调用,它可以根据命令传递的连接名称在这两个数据库之一中执行一些 SQL 查询。问题是我不知道如何使用连接名称作为参数来获取DBAL连接。

我使用 Symfony 3.4。

config.yml是:

#config.yml
doctrine:
    dbal:
        default_connection: prod
        connections:
        prod:
            driver: '%database_driver1%'
            host: '%database_host1%'
            port: '%database_port1%'
            dbname: '%database_name1%'
            user: '%database_user1%'
            password: '%database_password1%'
            charset: UTF8

        backup:
            driver: '%database_driver2%'
            host: '%database_host2%'
            port: '%database_port2%'
            dbname: '%database_name2%'
            user: '%database_user2%'
            password: '%database_password2%'
            charset: UTF8

米点有这样的服务:

<?php

namespace ErqBundle\Services;

use Doctrine\DBAL\Driver\Connection;

class ProcSQL {

    public function exSQL($conn_name)
    {
        // How to obtain the connection ???? 
        $conn=$this->getDoctrine()->getConnection($conn_name);
        // This doesn't work !!!

        $sql = "SELECT ....";
        $stmt = $conn->query($sql); 
    }

}

但我无法从 "prod" 或 "backup" 等连接名称(例如:$conn=$this->getDoctrine()->getConnection ($conn_name))

我让它工作的唯一方法是重新定义连接参数并像这样建立连接:

public function exSQL()
{


    $config = new \Doctrine\DBAL\Configuration();

    $connectionParams = array(
        'dbname' => 'dbname',
        'user' => 'user',
        'password' => 'password',
        'host' => 'prod_host',
        'driver' => 'pdo_mysql',
    );
    $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);


    $sql = "SELECT ...";
    $stmt = $conn->query($sql); 

    while ($row = $stmt->fetch()) {
        var_dump($row);
    }

    // This works !!!


}

提前致谢。

你可以像这样使用依赖注入:

use \Doctrine\ORM\EntityManager;
class ProcSQL {
    private $entityManager
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function exSQL($conn_name)
    {
        $conn = $this->entityManager->getConnection($conn_name);
    }
}

你需要这样声明你的服务(我不知道你是否使用自动装配):

ErqBundle\Services\ProcSql:
    class:     ErqBundle\Services\ProcSql
    arguments:
         - '@doctrine.orm.entity_manager'

如果你能接触到学说,你可以做类似的事情

$this->getDoctrine()->getManager('backup');

但是正如@AlessandroMinoccheri所说,最好直接注入it/them。

如果需要注入默认值,就用doctrine.orm.entity_manager,如果需要注入backup,就用doctrine.orm.backup_entity_manager

谢谢你们。在你的答案中思考我已经尝试过:

我补充道:

use \Doctrine\ORM\EntityManager;

并在构造方法中直接调用了Doctrine EntityManager

public function __construct(EntityManager $entityManager)
{
    $this->entityManager = $entityManager;
}

public function exSQL($conn_name)
{

    $conn = $this->entityManager->getConnection($conn_name);

    $sql = "SELECT ...";
    $stmt = $conn->query($sql); 

    while ($row = $stmt->fetch()) {
        var_dump($row);
    }   

} 

而且有效!!!!!

非常感谢!!!