配置多數(shù)據(jù)庫:
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框架)
如果在你的應用中應用了不止一個數(shù)據(jù)庫,且你需要給你的 AR 類使用不同的數(shù)據(jù)庫鏈接(DB connection) ,你可以覆蓋掉 yiidbActiveRecord::getDb() 方法:
class Customer extends ActiveRecord{ // ... public static function getDb() { return Yii::$app->db2; // 使用名為 "db2" 的應用組件 } }