Symfony Doctrine connection wrapper

后端 未结 1 1740
孤城傲影
孤城傲影 2021-01-21 20:37

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

相关标签:
1条回答
  • 2021-01-21 21:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题