title: Active Record in Yii2, Joining multiple table, find the rating of a thread, and retrieve the rating in ListView
body: <p>I am confused with how to retrieve data that has been averaged after joining multiple tables. </p>
  
  <p>Problem:
  I have 3 tables i want to join, User, Thread, and Rate.
  I need all data from the tables and for rate table, i need to do average operation to get the rating value</p>
  
  <p>My code is:
  MyController looks like this:</p>
  
  <pre><code>  public function actionHome(){
  
          $dataProvider = new ActiveDataProvider([
              'query' =&gt; Thread::retrieveAll(),            
              'pagination' =&gt; [
                  'pageSize' =&gt;5,
              ],
  
          ]);
  
          return $this-&gt;render('home', ['listDataProvider' =&gt; $dataProvider]);
      }
  </code></pre>
  
  <p>My active record class (model class) looks like this:</p>
  
  <pre><code>public static function retrieveAll(){
  
  
  
    return  Self::find()-&gt;joinWith('user')
      -&gt;joinWith('rate')
      -&gt;select(['AVG(rate.rating) as rating, thread.*, user.*'])
      -&gt;groupBy('rate.thread_id');    
  }
  
  public  function getUser(){
           return $this-&gt;hasOne(User::className(), ['id' =&gt; 'user_id']);
  
  }
  
  public function getRate(){
      return $this-&gt;hasMany(Rate::className(), ['thread_id' =&gt;'thread_id']);
  }
  </code></pre>
  
  <p>Retrieve all is used in the data provider query
  My home class looks like this:</p>
  
  <pre><code>&lt;div class="col-md-6"&gt;
      &lt;?= ListView::widget([
          'dataProvider' =&gt; $listDataProvider,
          'options' =&gt; [
              'tag' =&gt; 'div',
              'class' =&gt; 'list-wrapper',
              'id' =&gt; 'list-wrapper',
          ],
          'layout' =&gt; "{summary}\n{items}\n{pager}",
  
          'itemView' =&gt; function ($model, $key, $index, $widget) {
              return $this-&gt;render('_list_thread',['model' =&gt; $model]);
          }, 
          'pager' =&gt; [
              'firstPageLabel' =&gt; 'first',
              'lastPageLabel' =&gt; 'last',
              'nextPageLabel' =&gt; 'next',
              'prevPageLabel' =&gt; 'previous',
              'maxButtonCount' =&gt; 3,
          ],
      ]) ?&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>my item for the listview looks like this:</p>
  
  <pre><code>&lt;article&gt;
      &lt;div class="box col-md-12"&gt;
  
  
          &lt;div class="row"&gt;
                  &lt;?= Html::a($model-&gt;title, Url::to('thread/index.php?id='. $model-&gt;thread_id))?&gt;
          &lt;/div&gt;
          &lt;div class="row"&gt;
              &lt;p&gt; &lt;?= $model-&gt;content ?&gt; &lt;/p&gt; 
          &lt;/div&gt;
  
          &lt;div class="row"&gt;
  
          &lt;div class="col-md-5"&gt;
              &lt;?= StarRating::widget([
                  'name' =&gt; 'rating_2',
                  'value' =&gt; 2.5,
                  'readonly' =&gt; true,
                  'pluginOptions' =&gt; [
                      'showCaption' =&gt; false,
                      'min' =&gt; 0,
                      'max' =&gt; 5,
                      'step' =&gt; 1,
                      'size' =&gt; 'xs',
  
                  ]])?&gt;
          &lt;/div class="col-md-7" align="center center-vertical"&gt;
              &lt;p align="right" style="font-size:8px"&gt;Created by   &lt;?= $model-&gt;user-&gt;first_name?&gt;  &lt;?=$model-&gt;user-&gt;last_name?&gt; at &lt;?= $model-&gt;date_created ?&gt;&lt;/p&gt;
  
  
          &lt;/div&gt;
          &lt;/div&gt;
  
  
      &lt;br&gt;&lt;br&gt;&lt;br&gt;
  &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
  &lt;/article&gt;
  </code></pre>
  
  <p>I can retrieve $model->user->first_name and lst_name but i cannot retrieve $model->rate->rating</p>
  
  <p>Any suggestion for this?</p>