Yii2 GridView 为外国 Table 的相关 Table 实现值过滤和排序
Yii2 GridView implement Filter and Sort for Values for related Table of foreign Table
我有 3 个表:
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)
) ;
我用 Gii 为 tabCve
创建了一个 CRUD。
我成功地为 intCvePhaseID
等引用表中的字段实现了过滤和排序功能
现在我想通过 tabProgress
为 tabCategory
实现这个
tabCve
和tabProgress
之间的关系是1比1。
我必须如何在我的 SearchModel 中实现它?
到目前为止我做了什么:
模型中:
public function getProgress()
{
return $this->hasOne(TabProgress::className(),['intProgressCveID' => 'intCveID'])->with(['category']);
}
public function getCategory()
{
return $this->hasOne(TabCategory::className(),['intCategoryID' => 'intProgressCategoryID']);
}
在搜索模型中:
public 函数搜索($params)
{
$query = TabCve::find();
$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])
在您的 seachModel 中,您需要 public var 用于过滤器 ..
不需要 select 因为这是由 find.. 提供的,最好重新排列下面的代码
$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])
上面 link 中提供的示例建议使用 getter 来检索您需要的列,并在 andFilterWhere
中也使用此 getter
这样 :
在你的模型中你已经有一个
public function getCategory()
{
return $this->hasOne(TabCategory::className(),
['intCategoryID' => 'intProgressCategoryID']);
}
然后你可以为 strCategory
构建一个 getter
public function getStr_category() {
return $this->category->strCategory;
}
此时您可以使用
在模型搜索中检索数据
->andFilterWhere(['like','str_category', $this->intCveTypeID])
访问有关此问题的完整教程:
根据本教程,您应该:
1. 在搜索模型中将相关 table 属性设置为 public,如下所示:
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]);
在您的 view class 和 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'),
],
如果 table 关系是 hasOne()
你应该阅读本教程:
http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/
我有 3 个表:
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)
) ;
我用 Gii 为 tabCve
创建了一个 CRUD。
我成功地为 intCvePhaseID
现在我想通过 tabProgress
为 tabCategory
实现这个
tabCve
和tabProgress
之间的关系是1比1。
我必须如何在我的 SearchModel 中实现它?
到目前为止我做了什么:
模型中:
public function getProgress()
{
return $this->hasOne(TabProgress::className(),['intProgressCveID' => 'intCveID'])->with(['category']);
}
public function getCategory()
{
return $this->hasOne(TabCategory::className(),['intCategoryID' => 'intProgressCategoryID']);
}
在搜索模型中:
public 函数搜索($params) { $query = TabCve::find();
$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])
在您的 seachModel 中,您需要 public var 用于过滤器 ..
不需要 select 因为这是由 find.. 提供的,最好重新排列下面的代码
$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])
上面 link 中提供的示例建议使用 getter 来检索您需要的列,并在 andFilterWhere
中也使用此 getter这样 :
在你的模型中你已经有一个
public function getCategory()
{
return $this->hasOne(TabCategory::className(),
['intCategoryID' => 'intProgressCategoryID']);
}
然后你可以为 strCategory
构建一个 getterpublic function getStr_category() {
return $this->category->strCategory;
}
此时您可以使用
在模型搜索中检索数据->andFilterWhere(['like','str_category', $this->intCveTypeID])
访问有关此问题的完整教程:
根据本教程,您应该: 1. 在搜索模型中将相关 table 属性设置为 public,如下所示:
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]);
在您的 view class 和 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'),
],
如果 table 关系是 hasOne()
你应该阅读本教程:
http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/