配置多數(shù)據(jù)庫(kù):
return [ // ... 'components' => [ // ... 'db' => [ 'class' => 'yiidbConnection', 'dsn' => 'mysql:host=localhost;dbname=example', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], 'db2' => [ 'class' => 'yiidbConnection', 'dsn' => 'mysql:host=localhost;dbname=example', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], ], // ... ];
(推薦教程:yii框架)
如果在你的應(yīng)用中應(yīng)用了不止一個(gè)數(shù)據(jù)庫(kù),且你需要給你的 AR 類(lèi)使用不同的數(shù)據(jù)庫(kù)鏈接(DB connection) ,你可以覆蓋掉 yiidbActiveRecord::getDb() 方法:
class Customer extends ActiveRecord{ // ... public static function getDb() { return Yii::$app->db2; // 使用名為 "db2" 的應(yīng)用組件 } }