title: Filter by related field in GridView
body: <p>I have used <a href="http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/" rel="nofollow">http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/</a> tutorial and its great.</p>
  
  <p>Everything is working fine but I get stuck after adding "Scenario 3 Steps":</p>
  
  <pre><code>// filter by parent name
  $query-&gt;joinWith(['parent' =&gt; function ($q) {
          $q-&gt;where('parent.first_name LIKE "%' . $this-&gt;parentName . '%" ' .
          'OR parent.last_name LIKE "%' . $this-&gt;parentName . '%"');
  }]);
  </code></pre>
  
  <p>it fires mysql query like:</p>
  
  <pre><code>SELECT COUNT(*) FROM `person` LEFT JOIN `country` ON 
  `person`.`country_id` = `country`.`id` LEFT JOIN `person` `parent` ON 
  `person`.`id` = `parent`.`parent_id` WHERE parent.first_name LIKE "%%" OR
   parent.last_name LIKE "%%"
  </code></pre>
  
  <p>Which doesn't return any records.</p>
  
  <p>I have tried something like:</p>
  
  <pre><code>        if ($this-&gt;parentName) {
              $query-&gt;joinWith(['parent' =&gt; function ($q) {
                  $q-&gt;where('parent.first_name LIKE "%' . $this-&gt;parentName . '%" ' .
                  'OR parent.last_name LIKE "%' . $this-&gt;parentName . '%"');
              }]);
          }else {
              $query-&gt;joinWith('parent');
          }
  </code></pre>
  
  <p>but that's giving me an error like:</p>
  
  <pre><code>Trying to get property of non-object
  1. in /var/www/html/advanced/common/models/Person.php  at line 54
  /* Getter for parent name */
      public function getParentName() {
          return $this-&gt;parent-&gt;fullName;          // its 54th line
      }
  </code></pre>