Using Cgridview To List Item With Criteria

Hi,

I’m trying to use CGridView to list all the items of a user. But I can’t understand how…

In documentation, they explain how to get all the items :


$dataProvider=new CActiveDataProvider('Item');


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

    'dataProvider'=>$dataProvider,

));

http://www.yiiframework.com/doc/api/1.1/CGridView

But not how to filter them (by user or whatever)…

Ok, I just get it :

In controller :


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

                    'criteria' => array('condition' => 'created_by=' . Yii::app()->user->id,)));

in view :




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

    'dataProvider'=>$dataProvider,

));

Ok, here a new question :

rather than building my own action controler and my own view to list my items, I prefer to use the actionAdmin generated by yiic in ItemController and the admin.php view.

Here how I’m doing to restrict it to the user’s items :


	

	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new item('search');

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


                // MY MODIFICATION :

		$_GET['item']['created_by']= Yii::app()->user->id;





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

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


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

			'model'=>$model,

		));

	}

The line I added :

[color="#FF0000"]$_GET[‘item’][‘created_by’]= Yii::app()->user->id;[/color]

Am I doing right ?

I feel that this is not the best way to do… ???

there is nothing wrong with that but I would change the search method in my model since thats the method actionAdmin uses to filter data


	public function search()

	{

		...

		if (!Yii::app()->user->isGuest)

			$criteria->compare('created_by', Yii::app()->user->id);	

		...


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

ok, I’ll try that. Thanks