Customising the Admin action created by CRUD

Hi

The admin action of a controller which is generated by CRUD, by default shows all records of model when we click on manage link. I want to set a condition so that when clicked on manage, the admin action only shows record that satisfy some query criteria. where can i put criteria conditions for the admin query. the code of my admin action is given bellow, my controller name is MobileControler and model name is Mobile.

public function actionAdmin()

{


	$model=new Mobile('search');


	$model->unsetAttributes();  // clear any default values


	if(isset($_GET['Mobile']))


		$model->attributes=$_GET['Mobile'];





	$this->render('admin',array(


		'model'=>$model,


	));


}

In your model class Mobile

find method search

and after line


$criteria=new CDbCriteria

add


$criteria->condition='columnd_name =1';

where


columnd_name =1

your condition

Thanks,

What i need to do if i want to create a sperate action with this condition, so that existing admin action remains as it is. I Mean what i need to create new an action/ a model method / a view etc ?

  1. copy-paste your action and change only name of it, for example "actionList"

  2. change 2 lines of method search in your model to




public function search($param = array())

{

  $criteria=new CDbCriteria($param);

.....................................



  1. copy-paste your admin view and rename it to "list" then open it and change line

'dataProvider'=>$model->search(),

to


'dataProvider'=>$model->search(array('condition'=>'column_name=1')),

Great solution!

Just for the formality: add the new action to the accessRules and change the render in the new action to the new admin file


public function actionAdmin()

	{

		$model=new Person('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Person']))

			$model->attributes=$_GET['Person'];


		$this->render('admin',array(         //<----------here

			'model'=>$model,

		));

	}

Cheers,

Mark.

Just a small remark: If you have table relations (also used in search criteria),

and if tables have same column name, then it needs to be specified, ex:




'dataProvider'=>$model->search(array('condition'=>'t.column_name=1')),