2016-02-19 40 views
2

相關的表值我有3個表:Yii2 GridView控件執行篩選和排序爲外國表

CREATE TABLE tabCve 
(
    intCveID INTEGER NOT NULL AUTO_INCREMENT, 
    strNumber VARCHAR(20) NOT NULL, 
    fltScore FLOAT(0), 
    strDescription TEXT, 
    datImported DATETIME NOT NULL DEFAULT NOW(), 
    intCvePhaseID INTEGER, 
    intCveTypeID INTEGER, 
    PRIMARY KEY (intCveID), 
    KEY (intCvePhaseID), 
    KEY (intCveTypeID) 

) ; 


CREATE TABLE tabProgress 
(
    intProgressID INTEGER NOT NULL AUTO_INCREMENT, 
    intProgressCveID INTEGER NOT NULL, 
    intProgressUserID INTEGER NOT NULL, 
    intProgressStateID INTEGER NOT NULL, 
    intProgressCategoryID INTEGER, 
    datCreated DATETIME NOT NULL, 
    PRIMARY KEY (intProgressID), 
    KEY (intProgressCategoryID), 
    KEY (intProgressCveID), 
    KEY (intProgressStateID), 
    KEY (intProgressUserID) 

) ; 

CREATE TABLE tabCategory 
(
    intCategoryID INTEGER NOT NULL AUTO_INCREMENT, 
    strCategory VARCHAR(50) NOT NULL, 
    PRIMARY KEY (intCategoryID) 

) ; 

我已經創建了一個全球信息基礎設施的CRUD tabCve。 我是在執行領域的篩選和排序功能在引用的表像intCvePhaseID

全成現在我想通過tabProgress 實現此爲tabCategorytabCvetabProgress之間的關係爲1:1

怎麼辦我必須在我的SearchModel中實現它?

我做了什麼至今:

在Model:

public function getProgress() 
{ 
    return $this->hasOne(TabProgress::className(),['intProgressCveID' => 'intCveID'])->with(['category']); 
} 

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

在SearchModel:

公共職能搜索($ params)方法 { $查詢= TabCve: :找();

$dataProvider = new ActiveDataProvider([ 
    'query' => $query, 
    'sort'=> ['defaultOrder' => ['strNumber' => 'DESC']], 
]); 

$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]); 
//$query->andWhere(["intProgressID" => null]); 

$this->load($params); 

if (!$this->validate()) { 
    // uncomment the following line if you do not want to return any records when validation fails 
    // $query->where('0=1'); 
    return $dataProvider; 
} 

$query->andFilterWhere([ 
    'intCveID' => $this->intCveID, 
    'fltScore' => $this->fltScore, 
    'datImported' => $this->datImported, 
]); 

$query->andFilterWhere(['like', 'strNumber', $this->strNumber]) 
    ->andFilterWhere(['like', 'strDescription', $this->strDescription]) 
    ->andFilterWhere(['like','tabPhase.strPhase', $this->intCvePhaseID]) 
    ->andFilterWhere(['like','datImported',$this->datImported]) 
    ->andFilterWhere(['like','tabType.strType', $this->intCveTypeID]) 
    ->andFilterWhere(['like','tabProgress.tabCategory.strCategory', $this->intCveTypeID]) 
    ; 
return $dataProvider; 
} 

怎麼辦我要實現在這些線路中的字段:

$query->select(["intCveID","strNumber","fltScore","strDescription","datImported","intCvePhaseID","intCveTypeID",'progress.intProgressCategoryID']); 

和:

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

回答

1

在你seachModel你需要過濾公共變種..

不需要選擇,因爲這是由提供的查找..和更好的是重新排列下面

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

代碼以另一種方式等建議在這個文檔

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/

對於

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

樣品中的鏈接提供以上建議爲獲取你所需要的列,並使用該吸氣劑還andFilterWhere

使用吸氣劑是這樣的:

在模型中

你已經有一個

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

,那麼你可以在這一點上,你可以retri BUIL一個getter爲strCategory

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

前夜的數據,你modelSearch與

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

感謝您的反饋意見。我應該在這裏放置什麼: ' - > andFilterWhere(['like','tabProgress.tabCategory.strCategory',$ this-> intCveTypeID])' 因爲這種方法不起作用。 – bnu

+1

我已更新答案。我希望這是有用的 – scaisEdge

+0

接受這個答案,因爲它使我找到了正確的解決方案。 我將提供我的代碼作爲答案的完整解決方案。 – bnu

-1

訪問有關這個問題的一個完整的教程:根據本教程,您應該

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

: 1.設置你的相關表格屬性作爲公衆搜索模式如下:

public $groupname; 

2。包括在規則屬性:

public function rules() { 
     return [ 
      [['id', 'gender', 'status', 'sentstatus'], 'integer'], 
      [['groupname', 'addeddate', 'updateddate'], 'safe'], 
     ]; 
    } 
  • 改變默認查詢,以在功能search()以下代碼中搜索模型:

     
    public function search($params) { 
    $query = Contacts::find()->innerJoinWith('groups', true); 
    
    
  • 和andfilterwhere附加通緝屬性。像這樣:

     
    $query->andFilterWhere(['like', 'firstname', $this->firstname]) 
           ... 
           ->andFilterWhere(['like', 'groupname', $this->groupname]); 
    
  • 視圖類和GridView的你想要的列應該喜歡這個

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

    如果表關係hasOne()你應該閱讀本教程:

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

    +1

    歡迎您訪問解決方案的鏈接,但請確保您的答案在沒有它的情況下很有用:[添加鏈接上下文](// meta.stackexchange.com/a/8259),以便您的同行用戶可以瞭解它是什麼以及爲什麼它在那裏,然後引用您鏈接的頁面中最相關的部分,以防目標頁面不可用。 [答案只是一個鏈接,可能會被刪除。](// stackoverflow.com/help/deleted-answers) – sudo

    +0

    @sudo,非常感謝。我編輯了我的答案 –