久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

mysql-動(dòng)態(tài)更改數(shù)據(jù)庫(kù)連接Yii

 印度阿三17 2019-10-30

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

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

<?php
class 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”(擴(kuò)展CActiveRecord)并將db組件置于屬性中.他們?yōu)槭裁催@樣做?恐怕這種方式將數(shù)據(jù)庫(kù)連接建立的次數(shù)過(guò)多.

解決方法:

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

public static $conection; // Model attribute

public 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',
),
來(lái)源:https://www./content-2-535151.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多