PHP、Yii2 GridView 过滤关系值

PHP, Yii2 GridView filtering on relational value(PHP、Yii2 GridView 过滤关系值)
本文介绍了PHP、Yii2 GridView 过滤关系值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接下来:

Yii2 SearchModel 中的 search() 是如何工作的?

我希望能够过滤关系数据的 GridView 列.这就是我的意思:

我有两个表,TableATableB.两者都有使用 Gii 生成的相应模型.TableA 有一个指向 TableB 中的值的外键,像这样:

表AattrA1, attrA2, attrA3, TableB.attrB1表BattrB1, attrB2, attrB3

attrA1 和 attrB1 是它们对应表的主键.

现在,我有一个 attrA2attrA3attrB2 的 Yii2 GridView.我在 attrA2attrA3 上有一个工作过滤器,以便我可以搜索列值.我也对这两列进行了工作排序 - 只需单击列标题即可.我也希望能够在 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,但我不确定如何使用.同样,我也希望能够按此列排序 - 就像我可以通过单击标题链接对 attrA2AttrA3 进行排序一样.任何帮助,将不胜感激.谢谢.

解决方案

在 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",
], 

这篇关于PHP、Yii2 GridView 过滤关系值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)