Searching with respect to related active record column in GridView does not work

Searching with respect to related active record column in GridView does not work:

Hier is my scenario:

Database:




  tbl_carmanufacturer:


  `id` int(11) unsigned NOT NULL auto_increment,

  `abbr` varchar(20) NOT NULL,         	-- car manufacturer, abbreviation

  `designation` varchar(25) default NULL,  -- car manufacturer, designation

  `description` text default NULL,     	-- car manufacturer, description




 tbl_cartype:

  `id` int(11) unsigned NOT NULL auto_increment,

  `carmanufacturer_id` int(11) unsigned ,

  `abbr` varchar(25) NOT NULL,         	-- car type, abbreviation

  `designation` varchar(50) default NULL,  -- car type, designation

  `description` text default NULL,     	-- car type, description



Model




class Cartype extends  CActiveRecord

{

  .

  .

  .

  public function relations()

  {

  // NOTE: you may need to adjust the relation name and the related

  // class name for the relations automatically generated below.

    return array(

 	'carmanufacturer' => array(self::BELONGS_TO, 'Carmanufacturer', 'carmanufacturer_id'),

	'createUser' => array(self::BELONGS_TO, 'User', 'create_user_id'),

	'updateUser' => array(self::BELONGS_TO, 'User', 'update_user_id'),

	);

  }

  .

  .

  .


  public function search()

  {


    $criteria=new CDbCriteria;

    $criteria->with = array(

    'carmanufacturer' ,

    );

   

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

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

    $criteria->compare('carmanufacturer.designation',$this->carmanufacturer,true);

    $criteria->compare('t.abbr',$this->abbr,true);

    $criteria->compare('t.designation',$this->designation,true);

    $criteria->compare('t.fulldesignation',$this->fulldesignation,true);		


    $sort = new CSort;

    $sort->attributes = array(

   	'carmanufacturer' => array(

          'asc' => 'carmanufacturer.designation',

          'desc' => 'carmanufacturer.designation DESC',

        ),


   	'*',

    );


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

	'criteria'=>$criteria,

     'pagination'=>array(

             'pageSize'=>10

       ),


	'sort'=>$sort,


	));

   }


  public $carmanufacturer_name;

  

  public function afterFind() {	

   $this->carmanufacturer_name = $this->carmanufacturer->designation;

  }

	

}



views\cartype\admin.php





  .

  .

  .




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

 'id'=>'cartype-grid',

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

  'filter'=>$model,

  'columns'=>array(

		'id',

    //'carmanufacturer_id',

 	array(

 	'name'=>'carmanufacturer',

 	'type' => 'raw',

 	'value'=>'$data->carmanufacturer->designation',


 	),

		

    'abbr',

    'designation',

   //'description',

    array(

    'class'=>'CButtonColumn',

    ),

  ),

)); 




Every thing else just works fine, but trying to search Gridview with

respect to carmanufacturer yields no result: GridView stil displays

the same records after seaching that it was displaying before searching.

Does ples any one see where my mistake is? Any help is much appreciated.

thx.

follow this:

  1. add custom attribute in your model:



class Cartype extends CActiveRecord {

  ...

  public $carmanufacturer_search;

  ...

}



  1. add this attribute to rules applied on ‘search’ scope:



  public function rules() {

     return array(

        ...

        array( 'xxx,yyy,carmanufacturer_search', 'safe', 'on'=>'search' ),

  }



  1. update CGridView:



  'columns'=>array(

     ...

     array(

       'name'=>'carmanufacturer_search',

       'type' => 'raw',

       'value'=>'$data->carmanufacturer->designation',

     )



This should work. Also - if you want to add sorting on this column, you have to provide some configuration to dataProvider:




$criteria = CDbCriteria;

...

$criteria->with = array( "carmanufacturer" );


$dataProvider = new CActiveDataProvider( Cartype', array(

  'criteria'=>$criteria,

  'sort'=>array(

    'attributes'=>array(

      'carmanufacturer_search'=>array(

        'asc'=>'carmanufacturer.designation',

        'desc'=>'carmanufacturer.designation DESC',

      ),

      '*',

    ),

  ),

) );



remember to add ‘with=>array( “carmanufacturer” )’ to $criteria passed to dataProvider or it fail query database saying that ‘carmanufacturer’ alias is unknown. The last entry (’*’) in sort->attributes tells to treat all other columns normally. If you omit this option - you won’t be able to sort on any other column than defined here.

Hi redguy!

Thank you very much for your answer.

Have reproduced steps 1 through 3 of your

answer but stil, searching does not work!

Do I may be need something like this




public function search()

{

  .

  .

  .

 $criteria->compare('carmanufacturer_search.designation',$this->carmanufacturer,true);

  .

  .

  .

}



in my seach function im model?

Indeed… you must provide filter like this:




$criteria->compare('carmanufacturer.designation',$this->carmanufacturer_search,true);



I just forgot about this part :)

Redguy!

thank you very much.

Your hints helped me solve my problem in a very elegant way.

I do not know whether software development is your main occupation

but I think you are very talented to be a good lecturer in whatever subject.

D’you know, the most difficult thing sometimes is not to understand something

for oneself but to explain it to someone else in an understandable manner.

You are a master in explaining.

Once more, thx.