Answer the question
In order to leave comments, you need to log in
How to pass yii\db\Connection to an ActiveRecord connection?
There are several databases with the same tables and structure. There are models for user and post tables
class User extends ActiveRecord {
public static function tableName() {
return 'user';
}
public static function getDb() {
return Yii::$app->db1;
}
public function getPosts() {
return $this->hasMany(Post::class, ['author_id' => 'id']);
}
}
class Post extends ActiveRecord {
public static function tableName() {
return 'post';
}
public static function getDb() {
return Yii::$app->db1;
}
}
$user = User::find()->where(['id' => 1])->with('posts')->asArray()->one(Yii::$app->db2);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question