2015-03-13 17:20:20 11855次浏览 2条回答 2 悬赏 10 金钱

在网上搜到很多关于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',
];
补充于 2015-03-13 17:22

自己来解答,直接上代码。

创建链接测试用小程序如下:


namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use \yii\db\Connection;

class CmsSyncData extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '新闻ID',
        ];
    }
    /**
     * @inheritdoc
     */
    public static function getDb()
    {
        return new Connection([
            'dsn' => 'oci:dbname=//localhost:portno/dbname;charset=utf8',
            'username' => 'XXX',
            'password' => 'XXX',
        ]);
    }
    /**
     * Finds news by $id
     *
     * @param  string $id
     * @return static|null
     */
    public static function findByNewsId($id)
    {
        $connection = self::getDb();
        $command = $connection->createCommand('SELECT * FROM content_news WHERE id='.$id);
        $result = $command->queryOne();
        return $result;
    }

}

需要改YII2.0的代码:
\vendor\yiisoft\yii2\db\Connection.php
找到下面的函数,加上配置“, '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);
    }

最佳答案

您需要登录后才可以回答。登录 | 立即注册
kerin0364
见习主管

kerin0364

注册时间:2013-12-23
最后登录:2020-12-21
在线时长:11小时36分
  • 粉丝8
  • 金钱255
  • 威望0
  • 积分365

热门问题