problem with cgridview

Hi. I want to show every message written to the logged in user in a gridview. I have 83 messages in "Mensaje". only 4 belongs to current user. The gridview shows 1-10 from 4 messages in the summary and 10 messages y the data. If i sort by id column u can notice that gridview load all 83 messages. Any help?? This is the code fragment.

I wrote in the controler actionAdmin.

$model=new Mensaje(‘search’);

$model->unsetAttributes();

$dataProvider=new CActiveDataProvider(Mensaje::model()->with(

						array('destinatarios'=>array('condition'=>'id_usuario='. Yii::app()->user->id))


					));

$this->render(‘admin’,array(

		'model'=>$model,


		'dataProvider'=> $dataProvider,

));

In the above destinatarios is a relation.

I wrote in the view

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

	'id'=>'mensaje-grid',


	'dataProvider'=>$dataProvider,


	'filter'=>$model,


	'columns'=>array(


		'id',


		'usuario',


		'sistema',


		'texto',


		array(


			'class'=>$buttonColumn,


		),


	


	),


));

If you are trying to do filtering, then by following the Gii admin example, you shouldn’t be setting your own dataProvider, but rather using:


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

So if you want to force the user id each time, in your controller action you would have the following:


$model = new Mensaje('search');

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

if (isset($_GET['Mensaje'])) {

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

}

$model->id_usuario = Yii::app()->user->id;

$this->render('admin', array('model' => $model));

Hi RedRabbit. Thanks for your reply. U wrote

$model = new Mensaje(‘search’);

$model->id_usuario = Yii::app()->user->id;

but there is no id_usuario in “Mensaje” class. I use ‘destinatarios’ (a many_many relation) to get trought that property. That’s why i use my own dataprovider.

what should i do???

In which case I think I would suggest that you make a custom search function in your Mensaje model, that is an exact copy of the normal search function, but also either takes a CDbCriteria as parameter (which would allow you to preset the id_usuario when calling it, or that automatically includes it somehow.

For example:


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search($criteria = new CDbCriteria) {

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria->compare('id',$this->id,true);

		$criteria->compare('name',$this->name,true);

		

		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

then in your CGridview:


'dataProvider' => $model->search(new CDbCriteria(array(

	'with' => array(

		'destinatarios' => array(

			'condition' => 'id_usuario = :user',

		),

	),

	'params' => array(

		':user' => Yii::app()->user->id,

	),

))),

Obviously I don’t know your model attributes so just made those up, but just modify your existing search function as I did basically.