title: Dynamic doctrine database connection
body: <p>I have a project of an monitoring application under symfony 5.</p>
  <p>Currently I've done most of the work working with a single database containing logs from another application. What I'm looking to do now is to be able to connect dynamically to a database (MSSQL).
  For the moment the connection to the database is done via the file &quot;doctrine.yaml&quot;.</p>
  <p>Doctrine.yaml</p>
  <pre><code> doctrine:
    dbal:
     default_connection: default
      connections:
       default:
         driver: pdo_sqlsrv
         host: 192.168.1.33
         port: null
         dbname: 'job'
         user: 'sa'
         password: 'Lasernet@2020'
         charset: utf8mb4
  
     orm:
      default_entity_manager: default
      entity_managers:
       default:
         connection: default
         mappings:
          Main:
           is_bundle: false
           type: annotation
           dir: &quot;%kernel.project_dir%/src/Entity/Main&quot;
           prefix: 'App\Entity\Main'
           alias: default
  </code></pre>
  <p>I did this for the dynamic connection</p>
  <p>DynamicConnection.php</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\Doctrine;
  
  
  class DynamicConnection {
  
  
      public function __construct($dbname,$user,$password,$host,$driver,$port)
      {
          $this-&gt;dbname = $dbname;
          $this-&gt;user = $user;
          $this-&gt;password = $password;
          $this-&gt;host = $host;
          $this-&gt;driver = $driver;
          $this-&gt;port = $port;
  
      }
  
      public function changeDatabase(){
  
  
  
          $connectionParams = array(
              'dbname' =&gt; $this-&gt;dbname,
              'user' =&gt; $this-&gt;user,
              'password' =&gt; $this-&gt;password,
              'host' =&gt; $this-&gt;host,
              'driver' =&gt; $this-&gt;driver,
              'port' =&gt; $this-&gt;port
          );
  
          $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
      
          if($conn){
              return $conn;
          }else{
              return &quot;no&quot;;
          }
  
      }
  
  
      public function getParams()
      {
          $connectionParams = array(
              'driver' =&gt; $this-&gt;driver,
              'host' =&gt; $this-&gt;host,
              'port' =&gt; $this-&gt;port,
              'dbname' =&gt; $this-&gt;dbname,
              'user' =&gt; $this-&gt;user,
              'password' =&gt; $this-&gt;password,
              'charset' =&gt; &quot;utf8mb4&quot;,
              'driverOptions' =&gt; [],
              'defaultTableOptions' =&gt; []
          );
  
          return $connectionParams;
      }
  
  }
  </code></pre>
  <p>And in my Controller</p>
  <pre><code>/**
   * @Route(&quot;/testconnection&quot;, name=&quot;test_connect&quot;)
   */
  public function testConnection(){
  
  
      $dbname = &quot;job&quot;;
      $user = &quot;sa&quot;;
      $password = &quot;Lasernet@2020&quot;;
      $host = &quot;192.168.1.34&quot;;
      $driver = &quot;pdo_sqlsrv&quot;;
      $port = null;
  
      $connection = new DynamicConnection($dbname,$user,$password,$host,$driver,$port);
      $params = $connection-&gt;getParams();
  
  
      $newEm = EntityManager::create($params,$this-&gt;em-&gt;getConfiguration(), $this-&gt;em-&gt;getEventManager());
  
  
      $job = $newEm-&gt;getRepository(Job::class)-&gt;findAll();
      dd($job);
          
  }
  </code></pre>
  <p>The problem is that the findAll() returns all the records of the database &quot;192.168.1.33&quot; not the one of &quot;192.168.1.34&quot; which behaves a different number of records.</p>
  <p>Is there another way to connect dynamically to the database or to modify the &quot;doctrine.yaml&quot; file directly using JS for example, but I don't think this is the best solution.</p>
  <p>If someone has a solution to my problem to make my findAll() return the info from the 192 database
  .168.1.34</p>
  <p>Small precision the two databases have the same structure of tables, fields, etc.</p>