title: Yii2 gridview sorting
body: <p>I have a problem: <strong>I can't sorting my gridview.</strong><br>
  <br>When i click on "Product name":
  <a href="https://i.stack.imgur.com/rm3jU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rm3jU.png" alt="enter image description here"></a>
  In URL i can see : <strong>index?sort=-product_name</strong> but nothing happens. I did not use CRUD generator.<br><br>
  <strong>Controller</strong></p>
  
  <pre><code>public function actionIndex()
  {
      $searchModel = new CompanyProductSearch();
      $dataProvider = $searchModel-&gt;search(Yii::$app-&gt;request-&gt;queryParams);
      return $this-&gt;render('index', [
          'dataProvider' =&gt; $dataProvider,
          'searchModel' =&gt; $searchModel,
      ]);
  }
  </code></pre>
  
  <p><strong>SearchModel</strong></p>
  
  <pre><code>public $searchstring;
  
  public function rules()
  {
      return [
          [['date', 'product_name', 'searchstring'], 'safe'],
      ];
  }
  
  
  public function scenarios()
  {
      return Model::scenarios();
  }
  
  
  public function search($params)
  {
      $array = array();
      $user = Yii::$app-&gt;user-&gt;identity;
      $product_influencer = ProductInfluencer::find()-&gt;all();
      foreach($product_influencer as $product){
          $array[] .= $product-&gt;product_id;
      }
      $query = Product::find()-&gt;where(['company_id'=&gt;$user-&gt;company_id])
          -&gt;andWhere(['id'=&gt;$array])
          -&gt;andWhere(['is not', 'shop_price', null])
          -&gt;andWhere(['is not', 'main_category_id', null])
          -&gt;orderBy('date DESC');
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
          'pagination' =&gt; [
              'pageSize' =&gt; 10
          ],
      ]);
  
      $dataProvider-&gt;sort-&gt;attributes['product_name'] = [
          'asc' =&gt; ['product_name' =&gt; SORT_ASC],
          'desc' =&gt; ['product_name' =&gt; SORT_DESC],
      ];
  
  
  
      $this-&gt;load($params);
  
      if (!$this-&gt;validate()) {
          return $dataProvider;
      }
  
      $query-&gt;andFilterWhere([
          'product_name' =&gt; $this-&gt;product_name,
      ]);
  
  
      $query-&gt;andFilterWhere(['like', 'product_name', $this-&gt;searchstring]);
  
      return $dataProvider;
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;?php Pjax::begin(); ?&gt;
  &lt;?= GridView::widget([
      'summary'=&gt;"",
      'dataProvider' =&gt; $dataProvider,
      'tableOptions' =&gt; [
          'class' =&gt; 'table table-responsive',
          'id' =&gt; 'sortable-table',
      ],
      'pager' =&gt; [
          'class' =&gt; 'common\widgets\CustomPager',
          'prevPageLabel' =&gt; '&lt;div style="border: none" class="glyphicon glyphicon-menu-left"&gt;&lt;/div&gt;',
          'nextPageLabel' =&gt; '&lt;div style="border: none" class="glyphicon glyphicon-menu-right"&gt;&lt;/div&gt;',
          'maxButtonCount' =&gt; 0,
      ],
      'columns' =&gt; [
          ['class' =&gt; 'yii\grid\CheckboxColumn',],
          [
              'class' =&gt; 'yii\grid\SerialColumn',
              'header' =&gt; 'Nr.',
          ],
          [
              'format' =&gt; 'raw',
              'label' =&gt; 'Product name',
              'attribute' =&gt; 'product_name',
              'value' =&gt; function($model){
                  return Html::a($model-&gt;product_name, ['detail-product'], ['data' =&gt; [
                      'params'=&gt;['id'=&gt;$model-&gt;id],
                      'method' =&gt; 'get',
                  ]]);
              }
          ],
          [
              'label' =&gt; 'Total earnings',
              'value' =&gt; function($model){
                  return '$ 950 (test)';
              }
          ],
          [
              'label' =&gt; 'Units available',
              'value' =&gt; function($model){
                  $units = \common\models\ProductInfo::findOne(['product_id'=&gt;$model-&gt;id]);
                  return $units-&gt;shop_units;
              }
          ],
      ],
  ]); ?&gt;
  &lt;?php Pjax::end(); ?&gt;
  </code></pre>
  
  <p><strong>Thanks!</strong></p>