免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
mysql-動態(tài)更改數(shù)據(jù)庫連接Yii

我正在一個項目中,一個Yii安裝將運行多個網(wǎng)站.每個網(wǎng)站都有自己的數(shù)據(jù)庫,因此數(shù)據(jù)庫連接必須是動態(tài)的.

我做了什么,我創(chuàng)建了一個BeginRequestBehavior,它將在onBeginRequest中啟動.在這里,我將檢查已調(diào)用了哪個url,并確定匹配的數(shù)據(jù)庫(不在我的代碼中)并創(chuàng)建(或覆蓋)“ db”組件.

<?phpclass BeginRequestBehavior extends CBehavior{    /**     * Attaches the behavior object to the component.     * @param CComponent the component that this behavior is to be attached to     * @return void     */    public function attach($owner)    {        $owner->attachEventHandler('onBeginRequest', array($this, 'switchDatabase'));    }    /**     * Change database based on current url, each website has his own database.     * Config component 'db' is overwritten with this value     * @param $event event that is called     * @return void     */    public function switchDatabase($event)    {        // Here some logic to check which url has been called..        $db = Yii::createComponent(array(            'class' => 'CDbConnection',            'connectionString' => 'mysql:host=localhost;dbname=test',            'emulatePrepare' => true,            'username' => 'secret',            'password' => 'verysecret',            'charset' => 'utf8',            'enableProfiling' => true,            'enableParamLogging' => true        ));        Yii::app()->setComponent('db', $db);    }}

一切正常,但這是正確的方法嗎?在其他方法中,我看到人們?yōu)樗麄兊哪P蛣?chuàng)建自己的“ MyActiveRecord”(擴展CActiveRecord)并將db組件置于屬性中.他們?yōu)槭裁催@樣做?恐怕這種方式將數(shù)據(jù)庫連接建立的次數(shù)過多.

解決方法:

我使用模型函數(shù)定義她的數(shù)據(jù)庫連接:

public static $conection; // Model attributepublic function getDbConnection(){    if(self::$conection!==null)        return self::$conection;    else{        self::$conection = Yii::app()->db2; // main.php - DB config name        if(self::$conection instanceof CDbConnection){            self::$conection->setActive(true);            return self::$conection;        }        else            throw new CDbException(Yii::t('yii',"Active Record requires a '$conection' CDbConnection application component."));    }}

main.php:

 'db'=>array(     'connectionString' => 'mysql:host=192.168.1.*;dbname=database1',    'emulatePrepare' => true,    'username' => 'root',    'password' => 'password',    'charset' => 'utf8',    'enableProfiling'=>true,    'enableParamLogging' => true,),'db2'=>array(    'class'=>'CDbConnection',    'connectionString' => 'mysql:host=192.168.1.*;dbname=database2',    'emulatePrepare' => true,    'username' => 'root',    'password' => 'password',    'charset' => 'utf8',),
來源:https://www.icode9.com/content-2-535151.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Yii: 多數(shù)據(jù)庫配置和操作
Activiti工作流學習筆記(三)——自動生成28張數(shù)據(jù)庫表的底層原理分析
Tortoise ORM入門之理論篇
YII mysql footer 將Sql語句的執(zhí)行情況顯示在頁腳,便于排錯!
mysql 創(chuàng)建刪除數(shù)據(jù)庫
解決 PHP+MySQL 出現(xiàn)的中文亂碼
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服