title: CGridView Filter Multiple Models
body: <p>I have searched and searched for an answer to my problem but neither of them worked. My problem is regarding the filtering/searching in the CGridView for Yii.</p>
  
  <p>In my Applicant model I have the following:</p>
  
  <pre><code>private $_city = null;
  private $province_id = null;
  public function getCity()
      {
          if($this-&gt;_city === null &amp;&amp; $this-&gt;address_id !== null) {
              $this-&gt;_city = $this-&gt;address-&gt;city;
          }
  
          return $this-&gt;_city;
      }
  
      public function setCity($value)
      {
          $this-&gt;_city = $value;
      }
  
  public function search()
      {
          // Warning: Please modify the following code to remove attributes that
          // should not be searched.
  
          $criteria=new CDbCriteria;
  
          $criteria-&gt;with = array('address'=&gt;array('alias'=&gt;'address'));
  
          $criteria-&gt;compare('id',$this-&gt;id);
          $criteria-&gt;compare('phn',$this-&gt;phn);
          $criteria-&gt;compare('gender',$this-&gt;gender);
          $criteria-&gt;compare('dob',$this-&gt;dob,true);
          $criteria-&gt;compare('first_name',$this-&gt;first_name,true);
          $criteria-&gt;compare('last_name',$this-&gt;last_name,true);
          $criteria-&gt;compare('band_id',$this-&gt;band_id);
          $criteria-&gt;compare('note',$this-&gt;note,true);
          $criteria-&gt;compare('address.city',$this-&gt;city);
          $criteria-&gt;compare('address.province_id',$this-&gt;province_id);
  
          return new CActiveDataProvider($this, array(
              'criteria'=&gt;$criteria,
              'sort'=&gt;array(
                  'attributes'=&gt;array(
                      'id',
                      'phn',
                      'first_name',
                      'last_name',
                      'band_id',
                      'city'=&gt;array(
                          'asc'=&gt;'address.city ASC',
                          'desc'=&gt;'address.city DESC',
                      ),
                      'province_id'=&gt;array(
                          'asc'=&gt;'address.province_id ASC',
                          'desc'=&gt;'address.province_id DESC',
                      ),
                  )
              )
          ));
      }
  </code></pre>
  
  <p>In my View I have</p>
  
  <pre><code>$this-&gt;widget('zii.widgets.grid.CGridView', array(
          'id'=&gt;'applicants-grid',
          'dataProvider'=&gt;$model-&gt;search(),
          'filter'=&gt;$model,
          'columns'=&gt;array(
              'first_name',
              'last_name',
              'phn',
              array(
                  'name'=&gt;'band_id',
                  'value'=&gt;'$data-&gt;band-&gt;name',
                  'filter'=&gt;CHtml::listData(Band::model()-&gt;findAll(), 'id', 'name')
              ),
              array(
                  'name'=&gt;'city',
                  'value'=&gt;'$data-&gt;address-&gt;city', // causes 'Trying to get property of non-object' - no error if $data-&gt;city
                  'filter'=&gt;CHtml::listData(Address::model()-&gt;findAll(), 'city', 'city'),
              ),
              array(
                  'class'=&gt;'CButtonColumn',
                  'template'=&gt;'{view}',
                  'buttons'=&gt;array(
                      'view'=&gt;array(
                          'url'=&gt;'Yii::app()-&gt;createUrl("/applicant/view", array("id"=&gt;$data-&gt;id))',
                      )
                  )
              ),
          )
      ));
  </code></pre>
  
  <p>I have the cities and drop down list showing but when I choose are city from the dropdown it does not filter the widget with the proper results.</p>
  
  <p>I have tried doing the solutions in the following pages</p>
  
  <p><a href="http://www.yiiframework.com/forum/index.php?/topic/11546-cgridview-with-mulitple-model/" rel="nofollow">http://www.yiiframework.com/forum/index.php?/topic/11546-cgridview-with-mulitple-model/</a> 
  <a href="http://www.yiiframework.com/forum/index.php?/topic/19913-cgridview-with-multiple-models/" rel="nofollow">http://www.yiiframework.com/forum/index.php?/topic/19913-cgridview-with-multiple-models/</a></p>