dqwh0109 2016-05-20 07:23
浏览 40

过滤Admin中的关系模型字段 - CGridView

I am having difficulty getting the filtering to work in CGridView for relational fields from another model.

URL Reference: Yii 1.1: Searching and sorting by related model in CGridView

I follow the codes and it seems to be returning me the inputs from the other model. Everything looks fine but unfortunately, the filter is not working.

On Search, it will display a quick load icon but failed to filter accordingly. Upon checking further, I noticed the input is wrong. I am using Google Inspect Element and noticed the following:

<input name="User[full_name]" type="text">

I am using User model, relational to Biodata. Shouldn't it be Biodata[full_name]? If this is, where should I be looking at the codes?

Thank you. :D

  • 写回答

1条回答 默认 最新

  • dsn5510 2016-05-20 09:37
    关注

    Filtering and sorting in CGridView widget can be done in few steps:

    1. Add virtual field to your User model. It will be used to create column in CGridView; it also needed for proper filtering and sorting:

    class User extends CActiveRecord
    {
        public $bioFullName;
    
        //...
    }
    

    2. Modify search() function in your User model. You need to add array parameter to this function and add to $criteria object in this method connection with related model. To add sorting of related attribute, you also need to modify returned CActiveDataProvider. See what happens below:

    public function search($params = array()) // <-- new parameter that handling params for searching
    {
        $criteria = new CDbCriteria($param);
    
        $criteria->with = array('biodata'); //add relation with Biodata to $criteria object
    
        // ... existing $criteria conditions
    
        $criteria->addSearchCondition('biodata.full_name', $this->bioFullName, true, 'AND'); // add comparison of biodata.full_name
    
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            // ...
            'sort'=>array(
                'attributes'=>array(
                    'bioFullName'=>array( // <-- sorting of related field
                        'asc'=>'biodata.full_name ASC',
                        'desc'=>'biodata.full_name DESC',
                    ),
                    '*',
                ),
            ),
        ));
    }
    

    3. Adjust CGridView widget in your view to show related column. In columns array of widget configuration add column named like virtual field from model (bioFullName):

    <?php
    
    $this->widget('zii.widgets.grid.CGridView', array(
        // ... other widget configuration options
        'columns'=>array(
            // ... other columns
            'bioFullName'=>array(
                'name'=>'bioFullName', // <-- name of virtual model field
                'value'=>'$data->biodata->full_name', // <-- getting field value from relation
                'header'=>'Full name', // <-- CGridView column header
            ),
    
            // ... other columns
        ),
    ));
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统