在GridView Yii2中对数据进行排序和过滤,其中列不在数据库中

克里斯汀·格洛德

如果我在db中有2个字段-概率和影响,并且我需要在GridView中将这两个字段相乘的列。我设法像这样添加它:

    [
            'attribute' => 'priority',
            'format' => 'raw',
            'value' => function ($model) {
                return $model->influence * $model->probability;
            },
        ],

但是无法处理排序,因为该列不在db中,并且向$ query添加过滤器只会导致错误。

    $query = Risks::find();
    $query->select(`probability*influence AS priority`);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

已更新(适用于Asc和Desc,但不适用于过滤器)

public function search($params)
{
    $query = Risks::find();

    $query->joinWith(['author', 'proj']);

    $query->select('*, (probability * influence) as priority');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $dataProvider->setSort([
        'attributes' => [
           // 'id',
            'probability',
            'risks',
            'influence',
            'del' => [
                'asc' => ['risks.del' => SORT_ASC],
                'desc' => ['risks.del' => SORT_DESC],
            ],
            'priority' => [
                'asc' => ['priority' => SORT_ASC],
                'desc' => ['priority' => SORT_DESC],
                'label' => 'Priority',
            ],
            'proj' => [
                'asc' => ['projects.name' => SORT_ASC],
                'desc' => ['projects.name' => SORT_DESC],
            ],
            'author' => [
                'asc' => ['users.name' => SORT_ASC],
                'desc' => ['users.name' => SORT_DESC],
            ]
        ]
    ]);

    $this->load($params);

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


    $query->andFilterWhere([
        'id' => $this->id,
        'proj_id' => $this->proj_id,
        'author_id' => $this->author_id,
        'influence' => $this->influence,
        'probability' => $this->probability,
        //'del' => $this->del,
    ])
        ->andFilterWhere(['like', 'projects.name', $this->proj])
        ->andFilterWhere(['like', 'users.name', $this->author]);

    $query->andFilterWhere(['like', 'risks', $this->risks]);

    $query->having('priority = '. $this->priority);
    //$query->having(['priority' => $this->priority]);

    return $dataProvider;
}
deacs

步骤1:将吸气剂功能添加到基本Risks模型中:

public function getPriority() {
    return ($this->probability * $this->influence);
}

步骤2:priority为模型添加属性RisksSearch并配置规则。

/* your calculated attribute */
public $priority;

/* setup rules */
public function rules() {
   return [
       /* your other rules */
       [['priority'], 'safe']
   ];
}

步骤3:编辑search()方法以包括计算所得的字段 priority

public function search($params) {

    $query = Person::find();

    $query->select('*, (probability * influence) as priority');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    /**
     * Setup your sorting attributes
     * Note: This is setup before $this->load($params)
     */
    $dataProvider->setSort([
        'attributes' => [
            'id',
            'priority' => [
                'asc' => ['priority' => SORT_ASC],
                'desc' => ['priority' => SORT_DESC],
                'label' => 'Priority',
                'default' => SORT_ASC
            ],
        ]
    ]);
    ...

步骤4:添加$query->andFilterWhere()之后$this->load($params)才能过滤计算出的字段

// use the operator you wish, i.e. '=', '>', '<' etc
$query->andFilterWhere(['=', '(probability * influence)', $this->priority]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章