title: Symfony Doctrine connection wrapper
body: <p>I have implemented in symfony2 a connection wrapper to connect to database depending on the subdomain. I followed the instructions on the question <a href="https://stackoverflow.com/questions/6409167/symfony-2-multiple-and-dynamic-database-connection">Symfony 2 : multiple and dynamic database connection</a></p>
  
  <p>It works fine but when I try to update schema, symfony is selecting the name of the database (dinamic_database) I have in the configuration file instead of the name of the database (database1) I have in Session.</p>
  
  <p>The problem is that the value of the private property $params in class Doctrine\DBALConnection is created in the constructor and I need to change in my override connect method but I cant change this propety because dont have any methos setParams()</p>
  
  <blockquote>
    <p>config.yml</p>
  </blockquote>
  
  <pre><code>doctrine:
  dbal:
      default_connection: default
      connections:
          default:
              driver:   "%database_driver%"
              host:     "%database_host%"
              port:     "%database_port%"
              dbname:   "%database_name%"
              user:     "%database_user%"
              password: "%database_password%"
              charset:  UTF8
          cliente:
              wrapper_class: 'Em\Bundle\AppBundle\Connection\ConnectionWrapper'
              driver:   "%database_driver%"
              host:     "%database_host%"
              port:     "%database_port%"
              dbname:   'dinamic_database'
              user:     "%database_user%"
              password: "%database_password%"
              charset:  UTF8
  orm:
      auto_generate_proxy_classes: %kernel.debug%
      default_entity_manager: default
      entity_managers:
          default:
              connection: default
              mappings:
                  EmAdminBundle: ~
                  EmBackOfficeBundle: ~
                  EmContabilidadBundle: ~
  
              filters:
                  softdeleteable:
                      class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                      enabled: true
              dql:
                  string_functions:
                      MONTH: DoctrineExtensions\Query\Mysql\Month
                      YEAR: DoctrineExtensions\Query\Mysql\Year
          cliente:
              connection: cliente
              mappings:
                  EmAppBundle: ~
  </code></pre>
  
  <blockquote>
    <p>Connection wrapper</p>
  </blockquote>
  
  <pre><code>class ConnectionWrapper extends Connection
  {
  
  const SESSION_ACTIVE_DYNAMIC_CONN = 'active_dynamic_conn';
  
  /**
   * @var Session
   */
  private $session;
  
  /**
   * @var bool
   */
  private $_isConnected = false;
  
  /**
   * @var
   */
  private $defaultDatabaseName;
  
  /**
   * @param Session $sess
   */
  public function setSession(Session $sess)
  {
      $this-&gt;session = $sess;
  
  }
  
  
  /**
   * @param Session $sess
   */
  public function setDefaultDatabaseName($defaultDatabaseName)
  {
      $this-&gt;defaultDatabaseName = $defaultDatabaseName;
  }
  
  public function forceSwitch($dbName)
  {
  
      if ($this-&gt;session-&gt;has(self::SESSION_ACTIVE_DYNAMIC_CONN)) {
          $current = $this-&gt;session-&gt;get(self::SESSION_ACTIVE_DYNAMIC_CONN);
          if ($current[0] === $dbName) {
              return;
          }
      }
  
      $this-&gt;session-&gt;set(self::SESSION_ACTIVE_DYNAMIC_CONN, [
          $dbName
      ]);
  
      if ($this-&gt;isConnected()) {
          $this-&gt;close();
      }
  }
  
  public function forceDefault()
  {
      $this-&gt;forceSwitch($this-&gt;defaultDatabaseName);
  }
  
  /**
   * {@inheritDoc}
   */
  public function connect()
  {
  
      if (!$this-&gt;session-&gt;has(self::SESSION_ACTIVE_DYNAMIC_CONN)) {
          throw new \InvalidArgumentException('You have to inject into valid context first');
      }
      if ($this-&gt;isConnected()) {
          return true;
      }
  
      $driverOptions = isset($params['driverOptions']) ? $params['driverOptions'] : array();
  
      $params = $this-&gt;getParams();
      $realParams = $this-&gt;session-&gt;get(self::SESSION_ACTIVE_DYNAMIC_CONN);
      $params['dbname'] = $realParams[0];
  
      // $params['dbname'] is "database1"       
  
      $this-&gt;_conn = $this-&gt;_driver-&gt;connect($params, $params['user'], $params['password'], $driverOptions);
  
     $paramsAfterConection = $this-&gt;getParams();
     // $paramsAfterConection['dbname'] is "dinamic_database" 
  
      if ($this-&gt;_eventManager-&gt;hasListeners(Events::postConnect)) {
          $eventArgs = new ConnectionEventArgs($this);
          $this-&gt;_eventManager-&gt;dispatchEvent(Events::postConnect, $eventArgs);
      }
  
      $this-&gt;_isConnected = true;
  
      return true;
  }
  
  
  
  /**
   * {@inheritDoc}
   */
  public function isConnected()
  {
      return $this-&gt;_isConnected;
  }
  
  /**
   * {@inheritDoc}
   */
  public function close()
  {
      if ($this-&gt;isConnected()) {
          parent::close();
          $this-&gt;_isConnected = false;
      }
  }
  }
  </code></pre>
  
  <blockquote>
    <p>Database manager</p>
  </blockquote>
  
  <pre><code>class DatabaseManager
  {
  /**
   * @var EntityManager
   */
  private $emCliente;
  
  /**
   * @var ConnectionWrapper
   */
  private $connection;
  
  public function __construct(EntityManager $emCliente, ConnectionWrapper $connection)
  {
      $this-&gt;emCliente =  $emCliente;
      $this-&gt;connection = $connection;
  }
  
  public function createDatabaseApp(AppEvent $event) {
      $app = $event-&gt;getApp();
      $database = $app-&gt;getDatabaseName();
      $this-&gt;connection-&gt;getSchemaManager()-&gt;createDatabase($database);
      $this-&gt;connection-&gt;forceSwitch($database);
      $metadatas = $this-&gt;emCliente-&gt;getMetadataFactory()-&gt;getAllMetadata();
      $tool = new SchemaTool($this-&gt;emCliente);
      $tool-&gt;createSchema($metadatas);
      $this-&gt;connection-&gt;forceDefault();
  
  }
  
  public function deleteDatabaseApp(AppEvent $event)
  {
      $app = $event-&gt;getApp();
      $database = $app-&gt;getDatabaseName();
      if(in_array($database, $this-&gt;connection-&gt;getSchemaManager()-&gt;listDatabases())){
          $this-&gt;connection-&gt;getSchemaManager()-&gt;dropDatabase($database);
      }
  }
  
  public function updateSchema(App $app)
  {
      $database = $app-&gt;getDatabaseName();
      if(in_array($database, $this-&gt;connection-&gt;getSchemaManager()-&gt;listDatabases())){
          $this-&gt;connection-&gt;forceSwitch($database);
          $metadatas = $this-&gt;emCliente-&gt;getMetadataFactory()-&gt;getAllMetadata();
          $tool = new SchemaTool($this-&gt;emCliente);
  
          $sm = $this-&gt;connection-&gt;getSchemaManager();
  
          $fromSchema = $sm-&gt;createSchema();
          //fromSchema is trying to get information from dinamic_database instead of database1
  
          $toSchema = $tool-&gt;getSchemaFromMetadata($metadatas);
  
          $comparator = new Comparator();
          $schemaDiff = $comparator-&gt;compare($fromSchema, $toSchema);
          $sqls = $schemaDiff-&gt;toSql($this-&gt;connection-&gt;getDatabasePlatform());
  
      }
  }
  public function dropSchema(App $app)
  {
      $database = $app-&gt;getDatabaseName();
      if(in_array($database, $this-&gt;connection-&gt;getSchemaManager()-&gt;listDatabases())){
          $this-&gt;connection-&gt;forceSwitch($database);
          $this-&gt;connection-&gt;executeQuery('SET FOREIGN_KEY_CHECKS = 0');
          $metadatas = $this-&gt;emCliente-&gt;getMetadataFactory()-&gt;getAllMetadata();
          $tool = new SchemaTool($this-&gt;emCliente);
          $tool-&gt;dropSchema($metadatas);
          $this-&gt;connection-&gt;executeQuery('SET FOREIGN_KEY_CHECKS = 1');
          $this-&gt;connection-&gt;forceDefault();
      }
  }
  
  }
  </code></pre>