在网上搜到很多关于PDO连接Oracle的示例,但是实际使用中还是乱码了。数据库oracle的编码是utf8的,显示到页面却是gbk编码。
貌似连接串里的charset=utf-8未生效。
需要改YII2.0的代码:\vendor\yiisoft\yii2\db\Connection.php,在in_array里面加上oci
/**
* Initializes the DB connection.
* This method is invoked right after the DB connection is established.
* The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`
* if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty.
* It then triggers an [[EVENT_AFTER_OPEN]] event.
*/
protected function initConnection()
{
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($this->emulatePrepare !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
}
if ($this->charset !== null && in_array($this->getDriverName(), ['pgsql', 'mysql', 'mysqli', 'cubrid', 'oci'])) {
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));
}
$this->trigger(self::EVENT_AFTER_OPEN);
}
在配置数据库的地方做如下配置:
return [
'class' => 'yii\db\Connection',
'dsn' => 'oci:dbname=//db.cms.china.com:7137/deptdb;charset=utf8',
'username' => 'editor',
'password' => 'editor123',
];
必须顶,女中豪杰啊,美女程序员。
好棒,顶一个,大美女写的啊