PHP、Yii2 GridView 过滤关系值
接下来:
Yii2 SearchModel 中的 search() 是如何工作的?
我希望能够过滤关系数据的 GridView
列.这就是我的意思:
我有两个表,TableA
和 TableB
.两者都有使用 Gii 生成的相应模型.TableA
有一个指向 TableB
中的值的外键,像这样:
表AattrA1, attrA2, attrA3, TableB.attrB1表BattrB1, attrB2, attrB3
attrA1 和 attrB1 是它们对应表的主键.
现在,我有一个 attrA2
、attrA3
和 attrB2
的 Yii2 GridView
.我在 attrA2
和 attrA3
上有一个工作过滤器,以便我可以搜索列值.我也对这两列进行了工作排序 - 只需单击列标题即可.我也希望能够在 attrB2
上添加此过滤和排序功能.
我的 TableASearch
模型如下所示:
公共函数搜索($params){$query = TableA::find();$dataProvider = new ActiveDataProvider(['查询' =>$查询,]);如果 (!($this->load($params) && $this->validate())) {返回 $dataProvider;}$this->addCondition($query, 'attrA2');$this->addCondition($query, 'attrA2', true);$this->addCondition($query, 'attrA3');$this->addCondition($query, 'attrA3', true);返回 $dataProvider;}
在我的 TableA
模型中,我设置了这样的相关值
public $relationalValue;公共函数 afterFind(){$b = TableB::find(['attrB1' => $this->attrB1]);$this->relationalValue = $b->relationalValue;}
虽然这可能不是最好的方法.我想我必须在搜索功能的某个地方使用 $relationalValue,但我不确定如何使用.同样,我也希望能够按此列排序 - 就像我可以通过单击标题链接对 attrA2
和 AttrA3
进行排序一样.任何帮助,将不胜感激.谢谢.
在 Yii 2.0 中按列过滤 gridview 非常容易.请将过滤器属性添加到具有查找值的 gridview 列,如下所示:
<预><代码>[类" =>yiigridDataColumn::className(),属性" =>"status_id",'过滤器' =>ArrayHelper::map(Status::find()->orderBy('name')->asArray()->all(), 'id', 'name'),价值"=>功能($模型){if ($rel = $model->getStatus()->one()) {return yiihelpersHtml::a($rel->name,["crud/status/view", 'id' => $rel->id,],["data-pjax"=>0]);} 别的 {返回 '';}},格式"=>生的",],Following on from this:
Yii2 how does search() in SearchModel work?
I would like to be able to filter a GridView
column of relational data. This is what I mean:
I have two tables, TableA
and TableB
. Both have corresponding models generated using Gii. TableA
has a foreign key to a value in TableB
, like this:
TableA
attrA1, attrA2, attrA3, TableB.attrB1
TableB
attrB1, attrB2, attrB3
attrA1 and attrB1 are the primary keys of their corresponding tables.
Now, I have a Yii2 GridView
of attrA2
, attrA3
and attrB2
. I have a working filter on attrA2
and attrA3
so that I can search on column values. I also have a working sort for these two columns too - by just clicking on the column header. I would like to be able to add this filtering and sorting on attrB2
too.
My TableASearch
model looks like this:
public function search($params){
$query = TableA::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'attrA2');
$this->addCondition($query, 'attrA2', true);
$this->addCondition($query, 'attrA3');
$this->addCondition($query, 'attrA3', true);
return $dataProvider;
}
In my TableA
model, I set the related value like this
public $relationalValue;
public function afterFind(){
$b = TableB::find(['attrB1' => $this->attrB1]);
$this->relationalValue = $b->relationalValue;
}
Although it is probably not the best way of doing this. I think I have to use $relationalValue somewhere in my search function but I'm not sure how. Similarly, I would like to be able to sort by this column too - just like I can for attrA2
and AttrA3
by clicking on the header link`. Any help would be appreciated. Thanks.
Filtering a gridview by a column is damn easy in Yii 2.0. Please add the filter attribute to a gridview column having lookup values, as under:
[
"class" => yiigridDataColumn::className(),
"attribute" => "status_id",
'filter' => ArrayHelper::map(Status::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
"value" => function($model){
if ($rel = $model->getStatus()->one()) {
return yiihelpersHtml::a($rel->name,["crud/status/view", 'id' => $rel->id,],["data-pjax"=>0]);
} else {
return '';
}
},
"format" => "raw",
],
相关文章