CGridView: filters doesn't works

Hello, I have a problem :) (first sorry for my bad english :P)

I’ve created a grid: the pagination works perfectly. But the filters do not.

My controller is a module. (module Missive, controller Missive).




	public function actionIndex()

	{

		$model=new Missive('missiveRicevute');

		$model->unsetAttributes();

        

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

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


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

			'model'=>$model,

		));

	}






    public function missiveRicevute()

    {

		$criteria=new CDbCriteria;


		$criteria->compare('mittente_nome',$this->mittente_nome);

		$criteria->compare('testo',$this->testo);

        $criteria->compare('data', CDateTimeParser::parse($this->data, 'dd/MM/yyyy'));

        

        $criteria->condition = 'destinatario_id = :destinatario';

        $criteria->params = array(':destinatario' => Yii::app()->user->id);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

    }






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

	'id'=>'missive-grid',

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

	'filter'=>$model,

	'columns'=>array(

		array(

            'name' => 'data',

            'header' => 'Data',

            'value'=> 'CTimestamp::formatDate("d/m/Y", $data->data)'

        ),

		array(

            'name' => 'mittente_nome',

            'header' => 'Mittente',

        ),

		'testo',

		array(

			'class'=>'CButtonColumn',

            'template'=>'{view}{delete}',

		),

	),

)); ?>




What’s wrong? I’ve checked the ajax url in firebug when I use the filters but it is ok. I’ve no idea :(

Are all attributes ‘safe’ in the model rules, means the attributes are really assigned?




public function actionIndex()

        {

                $model=new Missive('missiveRicevute');

                $model->unsetAttributes();

        

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

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


                //do a var_dump here

                var_dump($model->attributes); die();


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

                        'model'=>$model,

                ));

        }






I’ve tried to type in filter “testo” this: sdf




array(6) { ["id"]=> NULL ["mittente_nome"]=> string(0) "" ["mittente_id"]=> NULL ["destinatario_id"]=> NULL ["testo"]=> string(3) "sdf" ["data"]=> string(0) "" }



Uff :(

The attributes are ok.

Now you have to investigate the criteria in the function missiveRicevute();

Do var_dump($criteria) there and check the data the dataProvider returns.

Check if they are filtered or not. If not, the kind of building the criteria is not ok.





 var_dump($criteria);

 $dataProvider = new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                ));


 var_dump($dataProvider->getData()); die();

  

  return $dataProvider; 



Solved.

I changed the criteria "position".




 public function missiveRicevute()

    {

                $criteria=new CDbCriteria;




        $criteria->condition = 'destinatario_id = :destinatario';

        $criteria->params = array(':destinatario' => Yii::app()->user->id);


                $criteria->compare('mittente_nome',$this->mittente_nome);

                $criteria->compare('testo',$this->testo);

        $criteria->compare('data', CDateTimeParser::parse($this->data, 'dd/MM/yyyy'));

        


                return new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                ));

    }



First condition, next criteria. Although I did not understand why: P

Now I understand why:

You have added conditions on $criteria->compare.

Afterwards you have cancelled all these conditions by setting $criteria->condition = ‘destinatario_id = :destinatario’ as the only condition in the criteria.

If you want to keep the conditions you have set before you have to use $criteria->addCondition(…).