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.