Yii2 使用另一数据库
1. 配置数据库
'db' => [ 'class' => 'yiidbConnection', 'dsn' => 'mysql:host=localhost;dbname=mydb', 'username' => 'user', 'password' => 'pass', 'charset' => 'utf8', ], 'db2' => [ 'class' => 'yiidbConnection', 'dsn' => 'mysql:host=localhost;dbname=db2', 'username' => 'user', 'password' => 'pass', 'charset' => 'utf8', ],
2. 重载方法
class Test extends ActiveRecord { public static function getDb() { return Yii::$app->db2; } }