title: How to connect 3 tables in yii2 and display in Gridview then make sorting work correctly
body: <p>I have used the gii tool to create crud application. I have 3 tables the tbl_targetcities, lib_cities, and lib_provinces. I was able to connect lib_cities to tbl_targetciteis but not the lib_provinces. And also the sorting of city / Municipality does not work. It seems that it sorts according ti the ID.</p>
  
  <p>tbl_target_cities</p>
  
  <p><img src="https://i.stack.imgur.com/HkfBX.png" alt="enter image description here"></p>
  
  <p>lib_cities</p>
  
  <p><img src="https://i.stack.imgur.com/HbdD3.png" alt="enter image description here"></p>
  
  <p>lib_provinces</p>
  
  <p><img src="https://i.stack.imgur.com/05akD.png" alt="enter image description here"></p>
  
  <p>sample View</p>
  
  <p><img src="https://i.stack.imgur.com/lyHUL.png" alt="enter image description here"></p>
  
  <p>So far here is my relation in the model.</p>
  
  <pre><code>public function getCityName()
  {
    return $this-&gt;hasOne(LibCities::className(),['city_code'=&gt;'city_code']);
  }
  </code></pre>
  
  <p>in my view file...</p>
  
  <pre><code>    &lt;?= GridView::widget([
      'dataProvider' =&gt; $dataProvider,
      'filterModel' =&gt; $searchModel,
      'columns' =&gt; [
          ['class' =&gt; 'yii\grid\SerialColumn'],
          [
              'attribute'=&gt;'city_code',
              'value'=&gt;'cityName.city_name'
          ],
                     [
              'attribute'=&gt;'prov code',
              'value'=&gt;'cityName.city_name'
          ],
          'kc_classification',
          'cluster',
          'grouping',
           'priority',
          'launch_year',
  
          ['class' =&gt; 'yii\grid\ActionColumn'],
      ],
  ]); ?&gt;
  </code></pre>
  
  <p>How to display the prov_name from lib_provinces???</p>
  
  <p><strong>EDIT to answer user2839376 question in the comment box</strong></p>
  
  <p>IN THE SEARCH MODEL CLASS    </p>
  
  <pre><code>$query = TblSpBub::find();
      $query-&gt;joinWith('brgyCode')-&gt;joinWith(['cityCode'])-&gt;joinWith(['cityCode.provCode']);
  
      $covered=  LibAreas::find()-&gt;where(['user_id'=&gt;yii::$app-&gt;user-&gt;identity-&gt;id])-&gt;all();
  
      $query-&gt;all();
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
          'sort'=&gt; ['defaultOrder' =&gt; ['id'=&gt;SORT_DESC]],
      ]);
  
      $dataProvider-&gt;sort-&gt;attributes['city'] = [
      'asc' =&gt; ['lib_Cities.city_name' =&gt; SORT_ASC],
      'desc' =&gt; ['lib_Cities.city_name' =&gt; SORT_DESC],
      ];
  
      $dataProvider-&gt;sort-&gt;attributes['province'] = [
      'asc' =&gt; ['lib_provinces.prov_name' =&gt; SORT_ASC],
      'desc' =&gt; ['lib_provinces.prov_name' =&gt; SORT_DESC],
      ];
  </code></pre>