2016-02-19 49 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,非常感谢。我编辑了我的答案 –