Beispiel #1
0
 /**
  * Создает DataProvider на основе переданных данных
  * @param $params - параметры
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = User::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $this::COUNT], 'sort' => array('defaultOrder' => ['created_at' => SORT_DESC])]);
     $this->load($params);
     // Если валидация не пройдена, то ничего не выводить
     if (!$this->validate()) {
         $query->where('0=1');
         return $dataProvider;
     }
     // Фильтр данных
     $query->andFilterWhere(['id' => $this->id, 'sex' => $this->sex, 'birthday' => $this->birthday, 'country_id' => $this->country_id, 'city_id' => $this->city_id, 'status' => $this->status, 'updated_at' => $this->updated_at, 'login_at' => $this->login_at]);
     if ($this->created_at) {
         $date = new \DateTime($this->created_at);
         $this->created_at = $date->format('Y-m-d');
     }
     $query->andFilterWhere(['like', 'first_name', $this->first_name])->andFilterWhere(['like', 'last_name', $this->last_name])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])->andFilterWhere(['like', 'email_confirm_token', $this->email_confirm_token])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'phone', $this->phone])->andFilterWhere(['like', 'created_at', $this->created_at])->andFilterWhere(['like', 'address', $this->address]);
     if ($this->id_from) {
         $query->andFilterWhere(['>=', 'id', $this->id_from]);
     }
     if ($this->id_till) {
         $query->andFilterWhere(['<=', 'id', $this->id_till]);
     }
     if ($this->created_at_from) {
         $date_from = new \DateTime($this->created_at_from);
         $query->andFilterWhere(['>=', 'created_at', $date_from->format('Y-m-d')]);
     }
     if ($this->created_at_till) {
         $date_till = new \DateTime($this->created_at_till);
         $query->andFilterWhere(['<=', 'created_at', $date_till->format('Y-m-d')]);
     }
     if ($this->login_at_from) {
         $date_from = new \DateTime($this->login_at_from);
         $query->andFilterWhere(['>=', 'login_at', $date_from->format('Y-m-d')]);
     }
     if ($this->login_at_till) {
         $date_till = new \DateTime($this->login_at_till);
         $query->andFilterWhere(['<=', 'login_at', $date_till->format('Y-m-d')]);
     }
     if ($this->birthday_from) {
         $birthday_from = new \DateTime($this->birthday_from);
         $query->andFilterWhere(['>=', 'birthday', $birthday_from->format('Y-m-d')]);
     }
     if ($this->birthday_till) {
         $birthday_till = new \DateTime($this->birthday_till);
         $query->andFilterWhere(['<=', 'birthday', $birthday_till->format('Y-m-d')]);
     }
     return $dataProvider;
 }