Using Cgridview To List Item With Criteria


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(



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(



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;







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(




ok, I’ll try that. Thanks