乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       印度阿三17 2019-10-30

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

      我做了什么,我創(chuàng)建了一個BeginRequestBehavior,它將在onBeginRequest中啟動.在這里,我將檢查已調(diào)用了哪個url,并確定匹配的數(shù)據(jù)庫(不在我的代碼中)并創(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ù)庫連接建立的次數(shù)過多.

      解決方法:

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

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

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多