title: custom search dropdown in yii
body: <p>I have a database containing 3 tables, <code>countries</code>, <code>cities</code> and <code>datacenters</code>. <code>country</code> is the parent of <code>city</code> and <code>city</code> is the parent of <code>datacenter</code>. In the <code>cities</code> I have added a search dropdown of countries and its working fine. I have added cites search dropdown in datacenters and its also working fine but now i want countries search dropdown in datacenters, how to do it.</p>
  
  <p>Code in datacenters for countries search dropdown in <code>admin.php</code>.</p>
  
  <pre><code>&lt;?php $this-&gt;widget('zii.widgets.grid.CGridView', array(
      'id'=&gt;'data-centers-grid',
      'dataProvider'=&gt;$model-&gt;search(),
      'filter'=&gt;$model,
      'columns'=&gt;array(
          'id',
          'city.cityname'=&gt;array(
              'name'=&gt;'cityid',
              'value'=&gt;'$data-&gt;city-&gt;cityname',
              'filter'=&gt;CHtml::listData(Cities::model()-&gt;findAll(array('order'=&gt;'cityname')), 'id', 'cityname')
          ),
          'city.country.countryname'=&gt;array(
              'name'=&gt;'city.country.id',
              'value'=&gt;'$data-&gt;city-&gt;countryid',
              'filter'=&gt;CHtml::listData(Countries::model()-&gt;findAll(array('order'=&gt;'countryname')), 'id', 'countryname')
          ),
          'datacentername',
          'datacentercode',
          'lastupdatedon',
          'createdon',
          array(
              'class'=&gt;'CButtonColumn',
              'template'=&gt; '{view}{update}',
          ),
      ),
  )); ?&gt;
  </code></pre>
  
  <h1>model code:</h1>
  
  <pre><code>class DataCenters extends CActiveRecord
  {
      [...]
  
      /**
       * @return array relational rules.
       */
      public function relations()
      {
          // NOTE: you may need to adjust the relation name and the related
          // class name for the relations automatically generated below.
          return array(
              'lastupdatedby0' =&gt; array(self::BELONGS_TO, 'Users', 'lastupdatedby'),
              'city' =&gt; array(self::BELONGS_TO, 'Cities', 'cityid'),
              'servers' =&gt; array(self::HAS_MANY, 'Servers', 'datacenterid'),
          );
      }
  
      [...]
  
      /**
       * Retrieves a list of models based on the current search/filter conditions.
       * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
       */
      public function search()
      {
          // Warning: Please modify the following code to remove attributes that
          // should not be searched.
  
          $criteria=new CDbCriteria;
  
          $criteria-&gt;compare('id',$this-&gt;id);
          $criteria-&gt;compare('cityid',$this-&gt;cityid);
  
          $criteria-&gt;compare('datacentername',$this-&gt;datacentername,true);
          $criteria-&gt;compare('datacentercode',$this-&gt;datacentercode,true);
          $criteria-&gt;compare('lastupdatedon',$this-&gt;lastupdatedon,true);
          $criteria-&gt;compare('lastupdatedby',$this-&gt;lastupdatedby);
          $criteria-&gt;compare('createdon',$this-&gt;createdon,true);
  
          return new CActiveDataProvider($this, array(
              'criteria'=&gt;$criteria,
          ));
      }
  
      [...]
  }
  </code></pre>