1

我已经将 main.php 配置为处理多个数据库,然后 gii 也给出错误我的 main.php 具有以下连接设置

'db'=>array(
        'class' => 'CDbConnection',
        'connectionString' => 'mysql:host='.$dbhost.';dbname='.DATABASENAME,
        'emulatePrepare' => true,
        'username' => YII_DB_UNAME,
        'password' => YII_DB_PASS,
        'charset' => 'utf8',
    ),

'db_transport'=>array(
        'class' => 'CDbConnection',
        'connectionString' => 'mysql:host='.$dbhost.';dbname='.YII_TRANSPORT_DB,
        'emulatePrepare' => true,
        'username' => YII_DB_UNAME,
        'password' => YII_DB_PASS,
        'charset' => 'utf8',
    ),

'db_admission'=>array(
        'class' => 'CDbConnection',
        'connectionString' => 'mysql:host='.$dbhost.';dbname='.YII_ADMISSION_DB,
        'emulatePrepare' => true,
        'username' => YII_DB_UNAME,
        'password' => YII_DB_PASS,
        'charset' => 'utf8',
    ),

'db_extendedsims'=>array(
        'class' => 'CDbConnection',
        'connectionString' => 'mysql:host='.$dbhost.';dbname='.YII_EXTENDEDSIMS_DB,
        'emulatePrepare' => true,
        'username' => YII_DB_UNAME,
        'password' => YII_DB_PASS,
        'charset' => 'utf8',
    ),

现在,当我使用 GII 并在数据库连接中写“db”(不带引号)时,它可以工作,但如果我写“db_extendedsims”,它就不起作用。我应该怎么做才能使它适用于所有连接

4

2 回答 2

0

使用 yii 时使用多个数据库连接的更酷的方法:

    'db' => array(
        'class' => 'PortalDbConnection',
        'connectionString' => 'mysql:host=localhost;dbname=mydb',
        'username' => 'dev',
        'password' => '123456',
        'tablePrefix' => '',
        'emulatePrepare' => true,
        'enableParamLogging' => true,
        'enableProfiling' => true,
        'charset' => 'utf8',
    ),
    'dbx' => array(
        'connectionString' => 'mysql:host=localhost;dbname=mydb1',
        'username' => 'dev',
        'password' => '123456',
        'charset' => 'utf8',
        'tablePrefix' => '',
        'autoConnect' => false,
        'class' => 'CDbConnection', //this may be the reason you get errors! always set this
    ),

现在,您的模型的 php 代码:

<?php

class DomainSlaveM extends Domain {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static $server_id = 1;
    public static $slave_db;

    public function getDbConnection() {
        self::$slave_db = Yii::app()->dbx;
        if (self::$slave_db instanceof CDbConnection) {
            self::$slave_db->active = false;
            $config = require(Yii::app()->getBasePath() . '/config/main.php');
//set new connection string; also, you may set the right db user and pass if needed
                $connectionString = 'mysql:host=localhost;dbname=mydb' . self::$server_id;
                self::$slave_db->connectionString = sprintf($connectionString, 'dbx');
                self::$slave_db->setActive(true);
                return self::$slave_db;
            }
            else
                throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
        }

    }

并像这样使用它:

    public function createDomainSlaveM($model) {
//switch the db connection; $server_id gets values, from 1 to N
            DomainSlaveM::$server_id = $model->user_id;
            $model_domain_slave_m = new DomainSlaveM();
            $model_domain_slave_m->attributes = $model->attributes;
            if ($model_domain_slave_m->validate() && $model_domain_slave_m->save()) {
                //ok
            } else {
                //bad
            }
        }
于 2013-10-30T08:55:03.160 回答
0

我的机器是x64,

我修改文件 C:\ProgramData\MySQL\MySQL Server 5.6\my.ini,将 mysql 端口从 3308 设置为 3306,这样就可以了!

于 2014-05-16T02:40:19.970 回答