title: Yii2 get column from junction table and use in gridview
body: <p>Part of my schema is a pictured:<img src="https://i.stack.imgur.com/ilkgR.png" alt="enter image description here"></p>
  
  <p>The junction table maps which items are in which category, with the "featured" column marking the item as featured in that category.</p>
  
  <p>What I need to do is get a list of all items in a specific category, including the "featured" attribute from the junction table and pass that data for display into a gridview.</p>
  
  <p>I have created a model for the <code>category_item</code> table and tried to implement the solution proposed <a href="https://stackoverflow.com/a/28784322/2271704">here</a> but how do I access and pass the data into the gridview?</p>
  
  <p><strong>Category Model</strong></p>
  
  <pre><code>public function getCategoryItems()
  {
      return $this-&gt;hasMany(CategoryItem::className(), ['category_id' =&gt; 'id']);
  }
  
  public function getItems()
  {
      return $this-&gt;hasMany(Item::className(), ['id' =&gt; 'item_id'])-&gt;viaTable('{{%category_item}}', ['category_id' =&gt; 'id']);
  }
  </code></pre>
  
  <p><strong>CategorySearch Model</strong></p>
  
  <pre><code>public function search($params)
  {
      $query = Category::find()-&gt;with('items')-&gt;with('categoryItems');
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  
      $this-&gt;load($params);
  
      if (!$this-&gt;validate()) {
          return $dataProvider;
      }
  
      $query-&gt;andFilterWhere([
          'id' =&gt; $this-&gt;id,
          'type' =&gt; $this-&gt;type,
      ]);
  
      $query-&gt;andFilterWhere(['like', 'title', $this-&gt;title])
          -&gt;andFilterWhere(['like', 'description', $this-&gt;description]);
  
      return $dataProvider;
  }
  </code></pre>
  
  <p><strong>Item Model</strong></p>
  
  <pre><code>public function getItemCategories()
  {
      return $this-&gt;hasMany(CategoryItem::className(), ['item_id' =&gt; 'id']);
  }
  
  public function getCategories()
  {
      return $this-&gt;hasMany(Category::className(), ['id' =&gt; 'category_id'])-&gt;viaTable('{{%category_item}}', ['item_id' =&gt; 'id']);
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function actionCategory($id = null)
  {
      if($id == null) {
          $dataProvider = new ActiveDataProvider([
              'query' =&gt; Category::find()-&gt;all(),
          ]);
  
          return $this-&gt;render('category', [
                  'dataProvider' =&gt; $dataProvider,
          ]);
      } else {
  
          $category = Category::findOne($id);
  
          if($category) {
              $searchModel = new CategorySearch();
  
              $itemsDataProvider = new ActiveDataProvider([
                  'query' =&gt;  $searchModel-&gt;search(['CategorySearch'=&gt;['id'=&gt;$id]]),
              ]);
  
              return $this-&gt;render('editCategory', [
                  'itemsDataProvider' =&gt; $itemsDataProvider
                  'model' =&gt; $category,
              ]);
          } else {
              throw new NotFoundHttpException('The requested page does not exist.');
          }
      }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;div class="col-md-12"&gt;
          &lt;h2&gt;Items&lt;/h2&gt;
          &lt;?= GridView::widget([
              'dataProvider' =&gt; $itemsDataProvider,
              'columns' =&gt; [
  
                  'id',
                  'name',
                  'categoryItems.id',
                  [
                      'attribute' =&gt; 'items.description',
                      'format' =&gt; 'html',
                      'value' =&gt; 'description',
                  ],
                  [
                      'label'=&gt;'Highlights',
                      'format'=&gt;'raw',
                      'value'=&gt;function($data) use ($model) {
                          if(TagFeaturedItem::find()-&gt;where(['item_id'=&gt;$data-&gt;id, 'tag_id'=&gt;$model-&gt;id])-&gt;all()) {
                              return Html::a('Remove as Highlighted', ['/highlights/category/'.$model-&gt;id.'/remove/'.$data-&gt;id], ['class'=&gt;'btn btn-xs btn-danger']);
                          } else {
                              return Html::a('Add as Highlighted',['/highlights/category/'.$model-&gt;id.'/add/'.$data-&gt;id], ['class'=&gt;'btn btn-xs btn-primary']);
                          }
                      },
                  ],
              ],
          ]); ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>In general the view file is a mix and match from trying to find a solution.
  The TagFeaturedItem model is from a previous version of the view, see <a href="https://stackoverflow.com/q/30411088/2271704">here</a></p>