[SOLVED] CActiveDataProvider utilizing search method

I needed to add conditions to a CGridView.

I did this with


$dataProvider=new CActiveDataProvider('Class', array(

    'criteria'=>array(

         'condition'=>'....'

    ),

));

requiring the change of


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

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

to


'dataProvider' => $dataProvider,

Now I loose the functionality of the search method. How can I regain that?

Why you don’t change just search() method in your model class, without adding criteria?

Because I want to limit the data that is displayed in my gridview, such as restricting to only the items that are active.

define a new search method in your model, say mySearch(). Take all the code from original search method, modify it to your needs and then use mysearch() in CGridView


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

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

You can pass the additional parameter to the search() method:




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

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



Of course, you will need to modify this method a bit ;)

The problem is that you can only define one dataprovider within CGridView. As I mentioned I had to change to


'dataProvider' => $dataProvider,

so if I define another dataprovider, CGridView only works off of the last one defined. Therefore no need to define a new search method.

Anybody else have any ideas with this?

Ok, thanks to MrSoundless here is a working solution.

Since I wanted to restrict my CGridView results to only items that I had as marked Active in my db all you need to do is add the following into your controller action. This eliminates having to define a new CActiveDataProvider.


$model->myProperty = 'myValue';

so my controller:


public function actionAdmin() {

  $model = new Orgs('search');

  $model->unsetAttributes();

  $model->StatusCode = 'A';


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

    $model->Attributes = $_GET['Orgs'];


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

    'model' => $model,

  ));

}