Yii2 And Two Databases


How to use two databases in Yii2?

How can use it in model?

I can use it as Yii::$app->db1, but I need create model, which use another database, than main(db)


If you are using multiple databases in your application and you want to use a different DB connection for your Active Record class, you may override the getDb() method:

class Customer extends ActiveRecord


    // ...

    public static function getDb()


        return \Yii::$app->db2;  // use "db2" application component