itstructure /
yii2-template-simple
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\models; |
||
| 4 | |||
| 5 | use yii\base\Model; |
||
| 6 | use yii\data\ActiveDataProvider; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * UserSearch represents the model behind the search form of `app\models\User`. |
||
| 10 | * |
||
| 11 | * @package app\models |
||
| 12 | */ |
||
| 13 | class UserSearch extends User |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @inheritdoc |
||
| 17 | */ |
||
| 18 | public function rules() |
||
| 19 | { |
||
| 20 | return [ |
||
| 21 | [ |
||
| 22 | [ |
||
| 23 | 'first_name', |
||
| 24 | 'last_name', |
||
| 25 | 'patronymic', |
||
| 26 | 'position', |
||
| 27 | 'login', |
||
| 28 | 'email', |
||
| 29 | 'phone', |
||
| 30 | ], |
||
| 31 | 'string', |
||
| 32 | ], |
||
| 33 | [ |
||
| 34 | [ |
||
| 35 | 'id', |
||
| 36 | 'public', |
||
| 37 | 'order', |
||
| 38 | ], |
||
| 39 | 'integer', |
||
| 40 | ], |
||
| 41 | [ |
||
| 42 | [ |
||
| 43 | 'created_at', |
||
| 44 | 'updated_at', |
||
| 45 | 'order', |
||
| 46 | ], |
||
| 47 | 'safe', |
||
| 48 | ], |
||
| 49 | ]; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritdoc |
||
| 54 | */ |
||
| 55 | public function scenarios() |
||
| 56 | { |
||
| 57 | // bypass scenarios() implementation in the parent class |
||
| 58 | return Model::scenarios(); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Creates data provider instance with search query applied |
||
| 63 | * |
||
| 64 | * @param array $params |
||
| 65 | * |
||
| 66 | * @return ActiveDataProvider |
||
| 67 | */ |
||
| 68 | public function search($params) |
||
| 69 | { |
||
| 70 | $query = User::find(); |
||
| 71 | |||
| 72 | // add conditions that should always apply here |
||
| 73 | |||
| 74 | $dataProvider = new ActiveDataProvider([ |
||
| 75 | 'query' => $query, |
||
| 76 | ]); |
||
| 77 | |||
| 78 | $dataProvider->setSort([ |
||
| 79 | 'attributes' => [ |
||
| 80 | 'order' => [ |
||
| 81 | 'asc' => ['order' => SORT_ASC], |
||
| 82 | 'desc' => ['order' => SORT_DESC], |
||
| 83 | 'label' => 'Order', |
||
| 84 | 'default' => SORT_ASC |
||
| 85 | ], |
||
| 86 | ] |
||
| 87 | ]); |
||
| 88 | |||
| 89 | $this->load($params); |
||
| 90 | |||
| 91 | if (!$this->validate()) { |
||
| 92 | // uncomment the following line if you do not want to return any records when validation fails |
||
| 93 | // $query->where('0=1'); |
||
| 94 | return $dataProvider; |
||
| 95 | } |
||
| 96 | |||
| 97 | // grid filtering conditions |
||
| 98 | $query->andFilterWhere([ |
||
| 99 | 'id' => $this->id, |
||
| 100 | 'order' => $this->order, |
||
| 101 | 'public' => $this->public, |
||
| 102 | 'created_at' => $this->created_at, |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 103 | 'updated_at' => $this->updated_at, |
||
|
0 ignored issues
–
show
The property
updated_at does not exist on app\models\UserSearch. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 104 | ]); |
||
| 105 | |||
| 106 | $query->andFilterWhere(['like', 'first_name', $this->first_name]) |
||
| 107 | ->andFilterWhere(['like', 'last_name', $this->last_name]) |
||
| 108 | ->andFilterWhere(['like', 'patronymic', $this->patronymic]) |
||
| 109 | ->andFilterWhere(['like', 'position', $this->position]) |
||
| 110 | ->andFilterWhere(['like', 'login', $this->login]) |
||
| 111 | ->andFilterWhere(['like', 'email', $this->email]) |
||
| 112 | ->andFilterWhere(['like', 'phone', $this->phone]); |
||
| 113 | |||
| 114 | $query->orderBy('order ASC'); |
||
| 115 | |||
| 116 | return $dataProvider; |
||
| 117 | } |
||
| 118 | } |
||
| 119 |