title: 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
body: <p>I have two databases one for production and one for backup purposes. I want to have a service in Symfony, called from a command, that can execute some SQL queries in one of these two databases depending on the name of the connecction passed by the command. The problem is that I don't know how to obtain the DBAL connection using the name of the connection as a parameter.</p>
  
  <p>I use Symfony 3.4.</p>
  
  <p>The config.yml is:</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>Mi idea is having a service like this:</p>
  
  <pre><code>&lt;?php
  
  namespace ErqBundle\Services;
  
  use Doctrine\DBAL\Driver\Connection;
  
  class ProcSQL {
  
      public function exSQL($conn_name)
      {
          // How to obtain the connection ???? 
          $conn=$this-&gt;getDoctrine()-&gt;getConnection($conn_name);
          // This doesn't work !!!
  
          $sql = "SELECT ....";
          $stmt = $conn-&gt;query($sql); 
      }
  
  }
  </code></pre>
  
  <p>But I haven't been able to obtain the connection from a name of connection like "prod" or "backup" (something like : $conn=$this->getDoctrine()->getConnection($conn_name) )</p>
  
  <p>The only way I have made it work is defining again the connection parameters and make the connection like this:</p>
  
  <pre><code>public function exSQL()
  {
  
  
      $config = new \Doctrine\DBAL\Configuration();
  
      $connectionParams = array(
          'dbname' =&gt; 'dbname',
          'user' =&gt; 'user',
          'password' =&gt; 'password',
          'host' =&gt; 'prod_host',
          'driver' =&gt; 'pdo_mysql',
      );
      $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
  
  
      $sql = "SELECT ...";
      $stmt = $conn-&gt;query($sql); 
  
      while ($row = $stmt-&gt;fetch()) {
          var_dump($row);
      }
  
      // This works !!!
  
  
  }
  </code></pre>
  
  <p>Thanks in advance.</p>