In The Fog A Bit. Need Help

I know, i know stupid question, but i am confused a bit. Need some help.

I have a defined a named scope in the deliverable model. How can i call it in this?




public function actionIndex()

	{

		$model=new Deliverable('search');

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

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

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


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

			'model'=>$model,

		));

	}




[s]Try




$model->criteria->scopes = array('scope1', 'scope2');



[/s]

Edit:

That’s not quite right as the criteria is a property of the CActiveDataProvider, not the model. Try setting the scopes in the CActiveDataProvider returned by the model’s search method.

Do you mean you want to only search on the results returned by your named scope?

Add to your search() method in your deliverable model:

[PHP]$criteria->scopes=array(‘myNamedScope’);[/PHP]

I would rather recommend you to add some value to the model attribute that works the same as the scope you want to apply, if you can.




public function actionIndex()

        {

                $model=new Deliverable('search');

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

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

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


                $model->xxx = $someValue; // a value that works the same as the scope


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

                        'model'=>$model,

                ));

        }



By doing this, your CGridView (or CListView) will show only the deliverables whose xxx is $someValue.

$model in this method is a container of search parameters. And the values of the attributes works as the filtering conditions.

The following wiki will be a help, I hope.

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider/

That would be the better option for simpler filters, however I have some really complicated scopes in my current project that would be a pain to rewrite into the controller, so I thought I’d recommend adding the scope to the search() method as that’d be a favourable option in the case of complicated scopes like some I’m working on at the moment.

Is there a major downside to doing that?

No, I don’t think so. I agree that the scopes are very handy.

I thought that filthy may want to understand CActiveDataProvider and Model::search() method clear enough to blow off the fog. So I wanted to show a simple way.

so much to learn. i understand a bit better. I understand how the GII generated actionIndex works.

So i decided to add the condition to the controller instead of a named scope. Simple and effective.

So i added:




public function actionIndex()

......

                // returns static model to be displayed (dataProvider)

                $id = Yii::App()->user->projectId;

                $model->project_id=$id;