sukhman4
(Sukhman4)
November 9, 2010, 10:41am
1
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,
));
}
rudenich
(Rudenich)
November 9, 2010, 10:59am
2
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
sukhman4
(Sukhman4)
November 9, 2010, 11:24am
3
rudenich:
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 ?
rudenich
(Rudenich)
November 9, 2010, 11:37am
4
copy-paste your action and change only name of it, for example "actionList"
change 2 lines of method search in your model to
public function search($param = array())
{
$criteria=new CDbCriteria($param);
.....................................
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')),
mark2bra
(Mark2bra)
January 21, 2014, 6:27pm
5
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.
erand
(Erandbraho)
December 27, 2014, 2:19pm
6
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')),