yii\data\ActiveDataProvider::setSort PHP Method

setSort() public method

public setSort ( $value )
    public function setSort($value)
    {
        parent::setSort($value);
        if (($sort = $this->getSort()) !== false && $this->query instanceof ActiveQueryInterface) {
            /* @var $model Model */
            $model = new $this->query->modelClass();
            if (empty($sort->attributes)) {
                foreach ($model->attributes() as $attribute) {
                    $sort->attributes[$attribute] = ['asc' => [$attribute => SORT_ASC], 'desc' => [$attribute => SORT_DESC], 'label' => $model->getAttributeLabel($attribute)];
                }
            } else {
                foreach ($sort->attributes as $attribute => $config) {
                    if (!isset($config['label'])) {
                        $sort->attributes[$attribute]['label'] = $model->getAttributeLabel($attribute);
                    }
                }
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Number::find();
     $query->with('owner', 'documents');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 30]]);
     $dataProvider->setSort(['attributes' => ['number', 'destination', 'comment'], 'defaultOrder' => ['number' => SORT_ASC]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     if (is_numeric($this->searchText)) {
         $query->andFilterWhere(['like', 'number', $this->searchText]);
     } elseif (!empty($this->searchText)) {
         $ownerId = [];
         foreach (Employee::findByName($this->searchText, true)->all() as $owner) {
             $ownerId[] = $owner->getPrimaryKey();
         }
         $query->andWhere(['in', 'ownerId', $ownerId]);
     }
     if ($this->operatorId !== self::OPERATOR_ANY) {
         $query->andFilterWhere(['operatorId' => $this->operatorId]);
     }
     if ($this->destination !== self::DESTINATION_ANY) {
         $query->andFilterWhere(['destination' => $this->destination]);
     }
     $query->andFilterWhere(['like', 'comment', $this->comment]);
     return $dataProvider;
 }
All Usage Examples Of yii\data\ActiveDataProvider::setSort