I have implemented in symfony2 a connection wrapper to connect to database depending on the subdomain. I followed the instructions on the question Symfony 2 : multiple and dynam
I finally solved the problem but dont know if it is the best way
I override the constructor method in connect method in ConnectionWrapper:
public function connect()
{
if (!$this->session->has(self::SESSION_ACTIVE_DYNAMIC_CONN)) {
throw new \InvalidArgumentException('You have to inject into valid context first');
}
if ($this->isConnected()) {
return true;
}
$driverOptions = isset($params['driverOptions']) ? $params['driverOptions'] : array();
$params = $this->getParams();
$realParams = $this->session->get(self::SESSION_ACTIVE_DYNAMIC_CONN);
$params['dbname'] = $realParams[0];
//overrride constructor in parent class Connection to set new parameter dbname
parent::__construct($params, $this->_driver, $this->_config,$this->_eventManager);
$this->_conn = $this->_driver->connect($params, $params['user'], $params['password'], $driverOptions);
if ($this->_eventManager->hasListeners(Events::postConnect)) {
$eventArgs = new ConnectionEventArgs($this);
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
}
$this->_isConnected = true;
return true;
}