Answer the question
In order to leave comments, you need to log in
Yii2, how to specify the use of another DB in the model?
you need to use a different database for a particular model.
in the komon in the main.php file I added a new database connection
'components' => [
'db2' => [
'class' => '\yii\db\Connection',
// ...
],
]
class Post extends ActiveRecord
{
public static function getDb()
{
return $this->db2;
}
}
Answer the question
In order to leave comments, you need to log in
According to the idea, you were correctly prompted to override the method for the AR model.
Try return Yii::$app->getComponent('db2');
or so return Yii::$app->db2;
I'll update Yii2 now and give it a try.
I just tried Yii::$app->db2 like this , it didn't work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question