Search Dropdown

i have a database containing 3 tables, countries, cities and datacenters. country is the parent of city and city is the parent of datacenter. in the cities 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.

here is my datacenters/admin.php code for view

<?php

/* @var $this DataCentersController */

/* @var $model DataCenters */

$this->breadcrumbs=array(

'Data Centers'=&gt;array('index'),


'Manage',

);

$this->menu=array(

array('label'=&gt;'List DataCenters', 'url'=&gt;array('index')),


array('label'=&gt;'Create DataCenters', 'url'=&gt;array('create')),

);

Yii::app()->clientScript->registerScript(‘search’, "

$(’.search-button’).click(function(){

&#036;('.search-form').toggle();


return false;

});

$(’.search-form form’).submit(function(){

&#036;.fn.yiiGridView.update('data-centers-grid', {


	data: &#036;(this).serialize()


});


return false;

});

");

?>

<h1>Data Centers</h1>

<?php $iHasFullAccess = $model->hasFullAccess(); if($iHasFullAccess) { ?> <a href="?r=datacenters/create" title="addnewdatacenter">Add New</a> <?php } ?>

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'data-centers-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'id',


	'city.cityname'=&gt;array('name'=&gt;'cityid', 'value'=&gt;'&#036;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;'&#036;data-&gt;city-&gt;countryid', 'filter'=&gt;CHtml::listData(Countries::model()-&gt;findAll(array('order'=&gt;'countryname')), 'id', 'countryname')),


	'datacentername',


	'datacentercode',


	'lastupdatedon',


	//array('name'=&gt;'lastupdatedby', 'value'=&gt;'&#036;data-&gt;lastupdatedby0-&gt;fullname'),


	'createdon',


	array(


		'class'=&gt;'CButtonColumn',


		'template'=&gt; (&#036;iHasFullAccess == 1 ? '{view}{update}' :  '{view}') ,


	),


),

)); ?>

model code for datacenters

==============================

<?php

/**

  • This is the model class for table "tbldatacenters".

  • The followings are the available columns in table ‘tbldatacenters’:

  • @property integer $id

  • @property string $datacentername

  • @property string $datacentercode

  • @property string $lastupdatedon

  • @property integer $lastupdatedby

  • @property string $createdon

  • The followings are the available model relations:

  • @property Tblusers $lastupdatedby0

  • @property Tblservers[] $tblservers

*/

class DataCenters extends CActiveRecord

{

/**


 * Returns the static model of the specified AR class.


 * @param string &#036;className active record class name.


 * @return DataCenters the static model class


 */


public static function model(&#036;className=__CLASS__)


{


	return parent::model(&#036;className);


}





/**


 * @return string the associated database table name


 */


public function tableName()


{


	return 'tbldatacenters';


}





/**


 * @return array validation rules for model attributes.


 */


public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('lastupdatedby, cityid', 'numerical', 'integerOnly'=&gt;true),


		array('datacentername, datacentercode', 'length', 'max'=&gt;100),


		array('lastupdatedon, createdon', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, datacentername, cityid, datacentercode, lastupdatedon, lastupdatedby, createdon', 'safe', 'on'=&gt;'search'),


	);


}





/**


 * @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'),


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'cityid'=&gt;'City',


		'datacentername' =&gt; 'Name',


		'datacentercode' =&gt; 'Code',


		'lastupdatedon' =&gt; 'Last Updated On',


		'lastupdatedby' =&gt; 'Last Updated By',


		'createdon' =&gt; 'Created On',


	);


}





/**


 * 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.





	&#036;criteria=new CDbCriteria;





	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('cityid',&#036;this-&gt;cityid);


	&#036;criteria-&gt;compare('datacentername',&#036;this-&gt;datacentername,true);


	&#036;criteria-&gt;compare('datacentercode',&#036;this-&gt;datacentercode,true);


	&#036;criteria-&gt;compare('lastupdatedon',&#036;this-&gt;lastupdatedon,true);


	&#036;criteria-&gt;compare('lastupdatedby',&#036;this-&gt;lastupdatedby);


	&#036;criteria-&gt;compare('createdon',&#036;this-&gt;createdon,true);





	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}





public function hasFullAccess()


{


	&#036;objUsers = new Users();


	return(&#036;objUsers-&gt;hasFullAccess());


}





public function checkDuplicateRecord(&#036;sDataCenterName, &#036;iDataCenterId = 0)


{


	&#036;iIsDuplicate = 0;


	&#036;sSearch = &quot;&quot;;


	if(&#036;iDataCenterId &gt; 0) &#036;sSearch = &quot; AND id &lt;&gt; '&#036;iDataCenterId'&quot;;


	if(Yii::app()-&gt;session['userid'])


	{


		&#036;criteria=new CDbCriteria;


		&#036;criteria-&gt;select='id';


		&#036;criteria-&gt;condition = &quot;datacentername='&quot;. &#036;sDataCenterName . &quot;'&quot;;


		if(&#036;sSearch &#33;= &quot;&quot;)


		{


			&#036;criteria-&gt;condition = 'id &lt;&gt; '. &#036;iDataCenterId . &quot; AND datacentername='&quot;. &#036;sDataCenterName . &quot;'&quot;;


		}


		


		&#036;sCities = DataCenters::model()-&gt;findAll(&#036;criteria);


		if(count(&#036;sCities) &gt; 0) return(1);


		else return(0);


	}


}





public function getCities()


{


	if(Yii::app()-&gt;session['userid'])


	{


		&#036;sQuery = &quot;SELECT C.countryname countryname, C.countrycode countrycode, C2.id id, C2.cityname cityname, C2.citycode citycode FROM tblcountries AS C INNER JOIN tblcities AS C2 ON C2.countryid = C.id WHERE C2.citystatus = '1'&quot;;


		


		&#036;connection = Yii::app()-&gt;db;


		&#036;command = &#036;connection-&gt;createCommand(&#036;sQuery);


		&#036;dataReader = &#036;command-&gt;query();


		&#036;data = &#036;dataReader-&gt;readAll();


		


		&#036;aCountries = array();


		foreach(&#036;data as &#036;row) &#036;aCountries[&#036;row['id']] = &#036;row['countryname'] . ' =&gt; ' . &#036;row['cityname'];


		


		return(&#036;aCountries);


	}


}

}

controller code for datacenters

================================

<?php

class DataCentersController extends Controller

{

/**


 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning


 * using two-column layout. See 'protected/views/layouts/column2.php'.


 */


public &#036;layout='//layouts/column1';





/**


 * @return array action filters


 */


public function filters()


{


	return array(


		'accessControl', // perform access control for CRUD operations


		'postOnly + delete', // we only allow deletion via POST request


	);


}





/**


 * Specifies the access control rules.


 * This method is used by the 'accessControl' filter.


 * @return array access control rules


 */


public function accessRules()


{


	return array(


		array('allow',  // allow all users to perform 'index' and 'view' actions


			'actions'=&gt;array('index','view'),


			'users'=&gt;array('*'),


		),


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('create','update'),


			'users'=&gt;array('*'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


			'users'=&gt;array('*'),


		),


		array('deny',  // deny all users


			'users'=&gt;array('*'),


		),


	);


}





/**


 * Displays a particular model.


 * @param integer &#036;id the ID of the model to be displayed


 */


public function actionView(&#036;id)


{


	&#036;this-&gt;render('view',array(


		'model'=&gt;&#036;this-&gt;loadModel(&#036;id),


	));


}





/**


 * Creates a new model.


 * If creation is successful, the browser will be redirected to the 'view' page.


 */


public function actionCreate()


{


	&#036;model = new DataCenters;


	if(isset(&#036;_POST['DataCenters']))


	{


		foreach(&#036;_POST['DataCenters'] as &#036;row=&gt;&#036;val)


		{


			if(&#036;row == 'datacentername')


			{


				&#036;iIsDuplicate = &#036;model-&gt;checkDuplicateRecord(&#036;val, 0);


				if(&#036;iIsDuplicate) &#036;this-&gt;redirect(array('create', 'error'=&gt;-1));


			}


			&#036;newPostedValues[&#036;row] = &#036;val;


		}


		


		&#036;model-&gt;attributes = &#036;newPostedValues;


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id, 'error'=&gt;0));


	}


	


	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Updates a particular model.


 * If update is successful, the browser will be redirected to the 'view' page.


 * @param integer &#036;id the ID of the model to be updated


 */


public function actionUpdate(&#036;id)


{


	&#036;model=&#036;this-&gt;loadModel(&#036;id);





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	&#036;newPostedValues = array();


	


	if(isset(&#036;_POST['DataCenters']))


	{


		foreach(&#036;_POST['DataCenters'] as &#036;row=&gt;&#036;val)


		{


			if(&#036;row == 'datacentername')


			{


				&#036;iIsDuplicate = &#036;model-&gt;checkDuplicateRecord(&#036;val, &#036;_GET['id']);


				if(&#036;iIsDuplicate) &#036;this-&gt;redirect(array('update', 'id'=&gt;&#036;_GET['id'],'error'=&gt;-1));


			}


			


			&#036;newPostedValues[&#036;row] = &#036;val;


		}


		&#036;model-&gt;attributes = &#036;newPostedValues;


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id, 'error'=&gt;1));


	}





	&#036;this-&gt;render('update',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Deletes a particular model.


 * If deletion is successful, the browser will be redirected to the 'admin' page.


 * @param integer &#036;id the ID of the model to be deleted


 */


public function actionDelete(&#036;id)


{


	&#036;this-&gt;loadModel(&#036;id)-&gt;delete();





	// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


	if(&#33;isset(&#036;_GET['ajax']))


		&#036;this-&gt;redirect(isset(&#036;_POST['returnUrl']) ? &#036;_POST['returnUrl'] : array('admin'));


}





/**


 * Lists all models.


 */


public function actionIndex()


{


	&#036;dataProvider=new CActiveDataProvider('DataCenters');


	&#036;this-&gt;render('index',array(


		'dataProvider'=&gt;&#036;dataProvider,


	));


}





/**


 * Manages all models.


 */


public function actionAdmin()


{


	&#036;model=new DataCenters('search');


	&#036;model-&gt;unsetAttributes();  // clear any default values


	if(isset(&#036;_GET['DataCenters']))


		&#036;model-&gt;attributes=&#036;_GET['DataCenters'];





	&#036;this-&gt;render('admin',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Returns the data model based on the primary key given in the GET variable.


 * If the data model is not found, an HTTP exception will be raised.


 * @param integer the ID of the model to be loaded


 */


public function loadModel(&#036;id)


{


	&#036;model=DataCenters::model()-&gt;findByPk(&#036;id);


	if(&#036;model===null)


		throw new CHttpException(404,'The requested page does not exist.');


	return &#036;model;


}





/**


 * Performs the AJAX validation.


 * @param CModel the model to be validated


 */


protected function performAjaxValidation(&#036;model)


{


	if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='data-centers-form')


	{


		echo CActiveForm::validate(&#036;model);


		Yii::app()-&gt;end();


	}


}

}

since you dont have direct relation between datacenter and countries you might have to implement relationship thru cities to get the countries attached

you are rit but how? plz write some code