title: Sort and filter data in GridView Yii2 where column is not in database
body: <p>If I have 2 fields in db - probability and influence and I need a column in GridView where these two fields are multiplied. I managed to add it there like:</p>
  
  <pre><code>    [
              'attribute' =&gt; 'priority',
              'format' =&gt; 'raw',
              'value' =&gt; function ($model) {
                  return $model-&gt;influence * $model-&gt;probability;
              },
          ],
  </code></pre>
  
  <p>But can't handle the sorting, because that column is not in db and adding filters to $query causes only errors.</p>
  
  <pre><code>    $query = Risks::find();
      $query-&gt;select(`probability*influence AS priority`);
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  </code></pre>
  
  <p><strong>Updated (works Asc and Desc but not with filters)</strong></p>
  
  <pre><code>public function search($params)
  {
      $query = Risks::find();
  
      $query-&gt;joinWith(['author', 'proj']);
  
      $query-&gt;select('*, (probability * influence) as priority');
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  
      $dataProvider-&gt;setSort([
          'attributes' =&gt; [
             // 'id',
              'probability',
              'risks',
              'influence',
              'del' =&gt; [
                  'asc' =&gt; ['risks.del' =&gt; SORT_ASC],
                  'desc' =&gt; ['risks.del' =&gt; SORT_DESC],
              ],
              'priority' =&gt; [
                  'asc' =&gt; ['priority' =&gt; SORT_ASC],
                  'desc' =&gt; ['priority' =&gt; SORT_DESC],
                  'label' =&gt; 'Priority',
              ],
              'proj' =&gt; [
                  'asc' =&gt; ['projects.name' =&gt; SORT_ASC],
                  'desc' =&gt; ['projects.name' =&gt; SORT_DESC],
              ],
              'author' =&gt; [
                  'asc' =&gt; ['users.name' =&gt; SORT_ASC],
                  'desc' =&gt; ['users.name' =&gt; SORT_DESC],
              ]
          ]
      ]);
  
      $this-&gt;load($params);
  
      if (!$this-&gt;validate()) {
          // uncomment the following line if you do not want to any records when validation fails
          // $query-&gt;where('0=1');
          return $dataProvider;
      }
  
  
      $query-&gt;andFilterWhere([
          'id' =&gt; $this-&gt;id,
          'proj_id' =&gt; $this-&gt;proj_id,
          'author_id' =&gt; $this-&gt;author_id,
          'influence' =&gt; $this-&gt;influence,
          'probability' =&gt; $this-&gt;probability,
          //'del' =&gt; $this-&gt;del,
      ])
          -&gt;andFilterWhere(['like', 'projects.name', $this-&gt;proj])
          -&gt;andFilterWhere(['like', 'users.name', $this-&gt;author]);
  
      $query-&gt;andFilterWhere(['like', 'risks', $this-&gt;risks]);
  
      $query-&gt;having('priority = '. $this-&gt;priority);
      //$query-&gt;having(['priority' =&gt; $this-&gt;priority]);
  
      return $dataProvider;
  }
  </code></pre>