微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在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',$this->risks]);

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

    return $dataProvider;
}
第1步:在基础风险模型中添加一个getter函数
public function getPriority() {
    return ($this->probability * $this->influence);
}

第2步:为模型RisksSearch添加属性优先级并配置规则.

/* your calculated attribute */
public $priority;

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

步骤3:编辑search()方法包括计算的字段优先级

public function search($params) {

    $query = Person::find();

    $query->select('*,]);

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

第4步:在$this-> load($params)之后添加$query-> andFilterWhere()以便能够过滤计算字段

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

原文地址:https://www.jb51.cc/php/133236.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐