Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii 如何在CGridView小部件中按相关对象进行过滤?_Yii_Cgridview - Fatal编程技术网

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);