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

后端 未结 2 1344
礼貌的吻别
礼貌的吻别 2021-01-22 16:04

I have 3 Tables:

CREATE TABLE tabCve
(
    intCveID INTEGER NOT NULL AUTO_INCREMENT,
    strNumber VARCHAR(20) NOT NULL,
    fltScore FLOAT(0),
    strDescriptio         


        
2条回答
  •  孤城傲影
    2021-01-22 16:11

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

提交回复
热议问题