yii\db\Connection::createPdoInstance PHP Method

createPdoInstance() protected method

This method is called by [[open]] to establish a DB connection. The default implementation will create a PHP PDO instance. You may override this method if the default PDO needs to be adapted for certain DBMS.
protected createPdoInstance ( ) : PDO
return PDO the pdo instance
    protected function createPdoInstance()
    {
        $pdoClass = $this->pdoClass;
        if ($pdoClass === null) {
            $pdoClass = 'PDO';
            if ($this->_driverName !== null) {
                $driver = $this->_driverName;
            } elseif (($pos = strpos($this->dsn, ':')) !== false) {
                $driver = strtolower(substr($this->dsn, 0, $pos));
            }
            if (isset($driver)) {
                if ($driver === 'mssql' || $driver === 'dblib') {
                    $pdoClass = 'yii\\db\\mssql\\PDO';
                } elseif ($driver === 'sqlsrv') {
                    $pdoClass = 'yii\\db\\mssql\\SqlsrvPDO';
                }
            }
        }
        $dsn = $this->dsn;
        if (strncmp('sqlite:@', $dsn, 8) === 0) {
            $dsn = 'sqlite:' . Yii::getAlias(substr($dsn, 7));
        }
        return new $pdoClass($dsn, $this->username, $this->password, $this->attributes);
    }

Usage Example

 /**
  * Creates the PDO instance.
  * When some functionalities are missing in the pdo driver, we may use
  * an adapter class to provides them.
  * @return \PDO the pdo instance
  */
 protected function createPdoInstance()
 {
     if (!empty($this->charset)) {
         Yii::trace('Error: Oci8PDO_Connection::$charset has been set to `' . $this->charset . '` in your config. The property is only used for MySQL and PostgreSQL databases. If you want to set the charset in Oracle to UTF8, add the following to the end of your Oci8PDO_Connection::$dsn: ;charset=AL32UTF8;', 'vendor\\sfedosimov\\yii2-oci8pdo\\Oci8PDO_Connection');
     }
     try {
         Yii::trace('Opening Oracle connection', 'vendor\\sfedosimov\\yii2-oci8pdo\\Oci8PDO_Connection');
         $pdoClass = parent::createPdoInstance();
     } catch (PDOException $e) {
         throw $e;
     }
     return $pdoClass;
 }