M
M
Maxim Grechushnikov2015-03-31 18:13:27
Yii
Maxim Grechushnikov, 2015-03-31 18:13:27

Yii2: How to make hasMany through cross table and count all records?

I do not quite understand how to make a selection through the crosstab.
There is a list of clubs. there are users who can be in several clubs at once.
You need to
1. count the number of users in each club
2. display a list of users on the club page

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Grechushnikov, 2015-03-31
@maxyc_webber

public function getMembers(){
    return $this->hasMany(\app\models\User::className(), ['id'=>'user_id'])
      ->viaTable('users_clubs', ['club_id'=>'id']);
  }

  public function getMembersCount(){
    return $this->getMembers()->count();
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question