Yii ExtendedActiveRecord 增強版 ActiveRecord 增加多資料庫連接綁定功能,activerecord_PHP教程

來源:互聯網
上載者:User

Yii ExtendedActiveRecord 增強版 ActiveRecord 增加多資料庫連接綁定功能,activerecord


ExtendedActiveRecord 繼承自 CActiveRecord,因此基礎功能與 CActiveRecord 無異

為添加對多資料庫連接的支援,增加了對 connectionName() 方法的回調,用法跟已有的 tableName() 方法一致,返回資料庫連接組件名稱的字串。

如果不定義該方法,則使用預設資料庫串連(db)

源碼如下:

class ExtendedActiveRecord extends CActiveRecord{    public static $db = array();    /**     * @return CDbConnection     * @throws CDbException     */    public function getDbConnection()    {        $componentName = $this->connectionName();        if (isset(self::$db[$componentName])) {            return self::$db[$componentName];        } else {            self::$db[$componentName] = Yii::app()->getComponent($componentName);            if (self::$db[$componentName] instanceof CDbConnection)                return self::$db[$componentName];            else {                $message = 'Active Record keyword requires a "' . $componentName . '" CDbConnection application component.';                Yii::log($message, CLogger::LEVEL_ERROR, 'extended');                throw new CDbException(Yii::t('yii', $message));            }        }    }    public function connectionName()    {        return 'db';    }}

執行個體:

class SomeModelClass extends ExtendedActiveRecord{    ......    public function connectionName() {        return 'some-db-connection';    }    ......}

  


Ralis串連遠端資料庫,提示ActiveRecord::ConnectionNotEstablished ,本地pl/sql可以訪問遠端資料庫

你是區域網路內可以遠端連線,廣域網路使用外網ip不可以串連嗎?
路由器上要映射ip地址和資料庫連接埠
 

yii20怎與XAMPP的資料庫連接?

yii2.0 官方提供兩個模板
先說基本版:
只要修改 config/db.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'hpcms_',
];

其中dbname是指資料庫名、 host 是主機、 tablePrefix 是表首碼

進階版的也差不多,修改 common/config/main-local.php
配置參數和上述基本一致!

這樣就可以連結資料庫了(當然你得要啟動mysql才可以,如果是其他資料庫,請搜尋一下,基本都是配置下參數即可)

對於如何操作資料庫(增刪改查)請看文檔ActiveRecord 以及Model (設計表結構後,可以用Gii快速產生Model)

想知道更多的話,看文檔最實際
 

http://www.bkjia.com/PHPjc/860062.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/860062.htmlTechArticleYii ExtendedActiveRecord 增強版 ActiveRecord 增加多資料庫連接綁定功能,activerecord ExtendedActiveRecord 繼承自 CActiveRecord,因此基礎功能與 CActiveReco...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.