title: PHP, Yii2 GridView filtering on relational value
body: <p>Following on from this:</p>
  
  <p><a href="https://stackoverflow.com/questions/21779085/yii2-how-does-search-in-searchmodel-work">Yii2 how does search() in SearchModel work?</a></p>
  
  <p>I would like to be able to filter a <code>GridView</code> column of relational data. This is what I mean:</p>
  
  <p>I have two tables, <code>TableA</code> and <code>TableB</code>. Both have corresponding models generated using  Gii. <code>TableA</code> has a foreign key to a value in <code>TableB</code>, like this:</p>
  
  <pre><code>TableA
  attrA1, attrA2, attrA3, TableB.attrB1
  
  TableB
  attrB1, attrB2, attrB3
  </code></pre>
  
  <p>attrA1 and attrB1 are the primary keys of their corresponding tables.</p>
  
  <p>Now, I have a Yii2 <code>GridView</code> of <code>attrA2</code>, <code>attrA3</code> and <code>attrB2</code>. I have a working filter on <code>attrA2</code> and <code>attrA3</code> so that I can search on column values. I also have a working sort for these two columns too - by just clicking on the column header. I would like to be able to add this filtering and sorting on <code>attrB2</code> too.</p>
  
  <p>My <code>TableASearch</code> model looks like this:</p>
  
  <pre><code>public function search($params){
      $query = TableA::find();
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  
      if (!($this-&gt;load($params) &amp;&amp; $this-&gt;validate())) {
          return $dataProvider;
      }
  
      $this-&gt;addCondition($query, 'attrA2');
      $this-&gt;addCondition($query, 'attrA2', true);
      $this-&gt;addCondition($query, 'attrA3');
      $this-&gt;addCondition($query, 'attrA3', true);
  
      return $dataProvider;
  }
  </code></pre>
  
  <p>In my <code>TableA</code> model, I set the related value like this</p>
  
  <pre><code>    public $relationalValue;
  
  public function afterFind(){
      $b = TableB::find(['attrB1' =&gt; $this-&gt;attrB1]);
      $this-&gt;relationalValue = $b-&gt;relationalValue;
  }
  </code></pre>
  
  <p>Although it is probably not the best way of doing this. I think I have to use $relationalValue somewhere in my search function but I'm not sure how. Similarly, I would like to be able to sort by this column too - just like I can for <code>attrA2</code> and <code>AttrA3</code> by clicking on the header link`. Any help would be appreciated. Thanks.</p>