Listdata From Model In Filters With Custom Input

Hi,

I have some cgridview table. I search my records using filters. Everything works fine. But I have one column hour_report with default values 1, 2, 3, 4.

Filtering from model with listdata works fine, code below:


array(

    'header'=>'Report from',

    'name'=>'hour_report',

    'filter'=>CHtml::listData(Raport::model()->findAll(array('order'=>'hour_report')),'hour_report','hour_report'),

    ),

This list returns me as input all fields from column hour_report ie. 1,2,3,4.

How can I add to listdata or to dropdownlist two custom inputs, so list as input should be like this: 1, 2, 3, 4, 1-2, 3-4.

Custom input 1-2 should return me all records which contains all 1 and 2.

Custom input 1-2 should return me all records which contains all 3 and 4.

I still haven’t found an answer.

When I manually try to add my custom inputs like this:




array(

'header'=>'Report from',

'name'=>'hour_report',

'filter'=>array('1'=>'7am-7pm','2'=>'8am-8pm','3'=>'7pm-7am','4'=>'8pm-8am',

                ''=>'All', ('1' || '2')=>'Daily',('3' || '4')=>'Nightly'),

),



cGridView works. It is searching values 1,2,3,4,All but 1-2, 3-4 doesn’t display properly.

Can I use operators like OR AND in filters?

You have to add the conditions for 1-2 and 3-4 in the models search() method.





array(

'header'=>'Report from',

'name'=>'hour_report',

'filter'=>array('1'=>'7am-7pm','2'=>'8am-8pm','3'=>'7pm-7am','4'=>'8pm-8am',

                ''=>'All', ('1-2')=>'Daily',('3-4')=>'Nightly'),

),








public function search()

{


$criteria=new CDbCriteria;


                if($this->hour_report == '1-2')               

                  $criteria->addCondition("(hour_report=1 OR hour_report=2)"); 

                elseif($this->hour_report == '3-4')                

                  $criteria->addCondition("(hour_report=3 OR hour_report=4)"); 

                else

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

               

                  ... other compare columns ....

	


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}






Maybe better use integer values as keys 12 and 34 as keys for Daily/Nightly,

because of validation hour_report -> integer ?





array(

'header'=>'Report from',

'name'=>'hour_report',

'filter'=>array('1'=>'7am-7pm','2'=>'8am-8pm','3'=>'7pm-7am','4'=>'8pm-8am',

                ''=>'All', ('12')=>'Daily',('34')=>'Nightly'),

),



Add the compare conditions for 12 and 34 in the model search() method:





public function search()

{


  $criteria=new CDbCriteria;


  if($this->hour_report == 12)

      $criteria->addCondition('(hour_report=1 OR hour_report=2)')  

  elseif($this->hour_report == 34)

      $criteria->addCondition('(hour_report=3 OR hour_report=4)')   

  else

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




   ... other compare conditions ...

   

   ... 



Thank You Joblo,

it works very good.