Yii2 GridView implement Filter and Sort for Values for related Table of foreign Table

时间秒杀一切 提交于 2019-12-02 00:32:15

In your seachModel you need public var for filter ..

not need select because this are provide by find.. and is better rearrange the code below

$query->select(["intCveID","strNumber","fltScore","strDescription","datImported","intCvePhaseID","intCveTypeID",'progress.intProgressCategoryID']);
$query->joinWith("phase");
$query->joinWith("type");
$query->joinWith("progress");
$query->Where(['not like', 'strDescription', '** RESERVED **%', false]);
$query->andWhere(['not like', 'strDescription', '** REJECT **%', false]);

in another way like suggested in this doc

http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/

For

->andFilterWhere(['like','tabProgress.tabCategory.strCategory', 
        $this->intCveTypeID])

the samples provide in the link above suggest the use of getter for retrieve the column you need and the use of this getter also in andFilterWhere

this way :

in your Model you already have a

public function getCategory()
{
    return $this->hasOne(TabCategory::className(),
     ['intCategoryID' =>   'intProgressCategoryID']);
}

then you can buil a getter for for strCategory

public function getStr_category() {
    return $this->category->strCategory;
}

at this point you can retrieve the data in you modelSearch with

->andFilterWhere(['like','str_category', $this->intCveTypeID])

visit a complete tutorial about this question in :

http://www.yiiframework.com/wiki/851/yii2-gridview-sorting-and-searching-with-a-junction-table-column-many-to-many-relationship/

according to this tutorial you should : 1. set your related table attribute as public in search model like this :

public $groupname;

2 . include attribute in the rules :

    public function rules() {
        return [
            [['id', 'gender', 'status', 'sentstatus'], 'integer'],
            [['groupname', 'addeddate', 'updateddate'], 'safe'],
        ];
    }
  1. change default query to following code in function search() in search model :

    public function search($params) {
    $query = Contacts::find()->innerJoinWith('groups', true);
    
    
  2. and in andfilterwhere add wanted attribute . like this :

    $query->andFilterWhere(['like', 'firstname', $this->firstname])
                   ... 
                    ->andFilterWhere(['like', 'groupname', $this->groupname]);
    

in your view class and in Gridview your wanted column should like this :

'columns' => [
                ...
            [
                'attribute' => 'tags',
                'format' => 'raw',
                'value' => function ($data) {
                    $groups= '';
                    foreach ($data->groups as $group) {
                        $groups .= '<a href="/group/' . $group->id . '">' .
                        $group->group_name . '</a> | ';
                    }
                    return $groups;
         },
        'filter' => ArrayHelper::map(group::find()->asArray()->all(), 'group_name','group_name'),
                ],

and if table relation is hasOne() you should read this tutorial :

http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!