【问题标题】:How to filter by a related object in CGridView widget?如何按 CGridView 小部件中的相关对象进行过滤?
【发布时间】:2013-12-06 21:36:05
【问题描述】:

我无法按网格中的相关对象进行过滤。

我有以下两种型号:

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 是数字,但表示(标签)是文本。所以我需要知道如何将此对象转换为网格中的过滤器。

【问题讨论】:

    标签: yii cgridview


    【解决方案1】:

    您必须在模型的搜索条件中包含外键:

    第一:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2013-05-06
      • 2014-06-16
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多