How do I exclude one attribute on admin view?

This seems simple but I haven’t found a solution after much searching. I have a Job model which refers to printing jobs. Among its attributes is a ‘status’ attribute that could be ‘new’, ‘on_press’ or ‘complete’. What I would like to on the job/admin page is to view every job where the status is not ‘complete’ without having to type in this condition into the filters at the top of the grid.

I’m guessing that I have to add the condition to the search function in Job model, but I’m not clear on the correct way to do this.

active record scopes to rescue here help yourself

http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes

If your action is index, you should have:




public function actionIndex()

{

     $model = new Job();


     $model->status = '<> "complete"';


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

     {

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

     }


     $dataProvider = new CActiveDataProvider($model);

    

     $this->render('index', array('dataProvider' => $dataProvider));

}



Solution is to set status before massive assigment. So this will be default value,

but if through grid user change status value, it will be assigned in massive assignment.

in Job model:


public function search($param=array())

{

   $criteria=new CDbCriteria($param);

.

.

.

}

and in admin view:




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'job-grid',

	'dataProvider' => $model->search(array('condition'=>'t.status=(value for new) || t.status=(value for on_press)')),

I created the scope “current” in the Job model. If I replace $model=new Job(‘search’); with $model=Job::model()->current()->findAll(); in job/controller I get an array to string conversion error.

I am using the admin action and tried using the code above replacing ‘index’ with ‘admin’. It would load the page with <>“complete” already in the filter box at the top of the grid but the filter was not applied. I took out the double quotes which put <>complete in the box which works if I type it in that way but does not work on page load.

Thank you, this worked for me. I was thinking I could find a way to express a negative condition but I don’t have many options so the || operator between all the positive options is fine.

In that case you should use the operator Fabrizio Caldarelli suggested: <>

Like this:


$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'job-grid',

        'dataProvider' => $model->search(array('condition'=>'t.status <> (value for complete)')),