M
M
Maxim2021-02-10 18:02:53
Yii
Maxim, 2021-02-10 18:02:53

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;
  }
}


How to make it so that when receiving data in this way
$user = User::find()->where(['id' => 1])->with('posts')->asArray()->one(Yii::$app->db2);

posts were also requested from db2?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question