Yii 如何在CGridView小部件中按相关对象进行过滤?
我有一个问题,无法按网格中的相关对象进行过滤 我有以下两种型号:Yii 如何在CGridView小部件中按相关对象进行过滤?,yii,cgridview,Yii,Cgridview,我有一个问题,无法按网格中的相关对象进行过滤 我有以下两种型号: Model Foo: id name create_date Model Boss: id foo_id (FK to Foo Model) create_date 现在,我以以下方式列出我的所有Boss对象: <?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'boss-gri
Model Foo:
id
name
create_date
Model Boss:
id
foo_id (FK to Foo Model)
create_date
现在,我以以下方式列出我的所有Boss对象:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'boss-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'ID',
array(
'header'=>'Foo', //the header cell text.
'name' => 'foo.name', //the attribute name of the data model.
'value' => '$data->foo->Name',
'filter' => CHtml::activeTextField(Foo::model(),'Name'),
),
'CREATE_DATE',
),
)); ?>
问题是FK是数字,但表示(标签)是文本。因此,我需要知道如何将此对象转换为网格中的过滤器。您必须在模型的搜索条件中包含外键: 第一:
array(
'header'=>'Foo',
'name' => 'foo.name',
'value' => '$data->foo->Name',
'filter' => CHtml::textField('foreign',''), // change like this
),
然后在模型中,搜索方法:
$criteria->with('NameOfRlation');
$f = Yii::app()->request->getParam('foreign' , null);
if(!empty($f))
$criteria->compare('t.Field' , $f , true);