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

前端之家收集整理的这篇文章主要介绍了php – 在GridView Yii2中对数据进行排序和过滤,其中列不在数据库中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我在db中有2个字段 – 概率和影响,我需要在GridView中使用这两个字段相乘的列.我设法在那里添加它:
  1. [
  2. 'attribute' => 'priority','format' => 'raw','value' => function ($model) {
  3. return $model->influence * $model->probability;
  4. },],

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

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

更新(使用Asc和Desc但不使用过滤器)

  1. public function search($params)
  2. {
  3. $query = Risks::find();
  4.  
  5. $query->joinWith(['author','proj']);
  6.  
  7. $query->select('*,(probability * influence) as priority');
  8.  
  9. $dataProvider = new ActiveDataProvider([
  10. 'query' => $query,]);
  11.  
  12. $dataProvider->setSort([
  13. 'attributes' => [
  14. // 'id','probability','risks','influence','del' => [
  15. 'asc' => ['risks.del' => SORT_ASC],'desc' => ['risks.del' => SORT_DESC],'priority' => [
  16. 'asc' => ['priority' => SORT_ASC],'desc' => ['priority' => SORT_DESC],'label' => 'Priority','proj' => [
  17. 'asc' => ['projects.name' => SORT_ASC],'desc' => ['projects.name' => SORT_DESC],'author' => [
  18. 'asc' => ['users.name' => SORT_ASC],'desc' => ['users.name' => SORT_DESC],]
  19. ]
  20. ]);
  21.  
  22. $this->load($params);
  23.  
  24. if (!$this->validate()) {
  25. // uncomment the following line if you do not want to any records when validation fails
  26. // $query->where('0=1');
  27. return $dataProvider;
  28. }
  29.  
  30.  
  31. $query->andFilterWhere([
  32. 'id' => $this->id,'proj_id' => $this->proj_id,'author_id' => $this->author_id,'influence' => $this->influence,'probability' => $this->probability,//'del' => $this->del,])
  33. ->andFilterWhere(['like','projects.name',$this->proj])
  34. ->andFilterWhere(['like','users.name',$this->author]);
  35.  
  36. $query->andFilterWhere(['like',$this->risks]);
  37.  
  38. $query->having('priority = '. $this->priority);
  39. //$query->having(['priority' => $this->priority]);
  40.  
  41. return $dataProvider;
  42. }
第1步:在基础风险模型中添加一个getter函数
  1. public function getPriority() {
  2. return ($this->probability * $this->influence);
  3. }

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

  1. /* your calculated attribute */
  2. public $priority;
  3.  
  4. /* setup rules */
  5. public function rules() {
  6. return [
  7. /* your other rules */
  8. [['priority'],'safe']
  9. ];
  10. }

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

  1. public function search($params) {
  2.  
  3. $query = Person::find();
  4.  
  5. $query->select('*,]);
  6.  
  7. /**
  8. * Setup your sorting attributes
  9. * Note: This is setup before $this->load($params)
  10. */
  11. $dataProvider->setSort([
  12. 'attributes' => [
  13. 'id','default' => SORT_ASC
  14. ],]
  15. ]);
  16. ...

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

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

猜你在找的PHP相关文章