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...