OSDN Git Service

Добавление Мест размещения и регионов (подразделений)
[invent/invent.git] / models / ItemsSearch.php
1 <?php
2
3 namespace app\models;
4
5 use yii\base\Model;
6 use yii\data\ActiveDataProvider;
7 use app\models\Items;
8 use app\models\Status;
9 use app\models\Types;
10 use app\models\Locations;
11 use app\models\Regions;
12
13 /**
14  * ItemsSearch represents the model behind the search form of `app\models\Items`.
15  */
16 class ItemsSearch extends Items
17 {
18     /**
19      * {@inheritdoc}
20      */
21     public function rules()
22     {
23         return [
24             [['id', 'state_id', 'type_id'], 'integer'],
25             [['name', 'model', 'os', 'mac', 'serial', 'product', 'modelnumber', 'invent', 'date', 'comment', 'statusName', 'typeName', 'locationName', 'regionName'], 'safe'],
26         ];
27     }
28
29     /**
30      * {@inheritdoc}
31      */
32     public function scenarios()
33     {
34         // bypass scenarios() implementation in the parent class
35         return Model::scenarios();
36     }
37
38     /**
39      * Creates data provider instance with search query applied
40      *
41      * @param array $params
42      *
43      * @return ActiveDataProvider
44      */
45     public function search($params)
46     {
47         $query = Items::find();
48         $query->joinWith(['status']);
49         $query->joinWith(['types']);
50         $query->joinWith(['locations']);
51 //        $query->joinWith(['regions']);
52
53         // add conditions that should always apply here
54
55         $dataProvider = new ActiveDataProvider([
56             'query' => $query,
57         ]);
58
59         $dataProvider->setSort([
60             'defaultOrder' => [
61                 'id' => SORT_ASC,
62             ],
63         ]);
64
65         $this->load($params);
66
67         if (!$this->validate()) {
68             // uncomment the following line if you do not want to return any records when validation fails
69             // $query->where('0=1');
70             return $dataProvider;
71         }
72
73         // grid filtering conditions
74         $query->andFilterWhere([
75             'id' => $this->id,
76             'date' => $this->date,
77         ])->andFilterWhere([
78             'like', Status::tableName().'.name', $this->statusName
79         ])->andFilterWhere([
80             'like', Types::tableName().'.name', $this->typeName
81         ])->andFilterWhere([
82             'like', Locations::tableName().'.name', $this->locationName
83 //        ])->andFilterWhere([
84 //            'like', Regions::tableName().'.name', $this->regionName
85         ]);
86
87         $query->andFilterWhere(['ilike', 'name', $this->name])
88             ->andFilterWhere(['ilike', 'model', $this->model])
89             ->andFilterWhere(['ilike', 'os', $this->os])
90             ->andFilterWhere(['ilike', 'mac', $this->mac])
91             ->andFilterWhere(['ilike', 'serial', $this->serial])
92             ->andFilterWhere(['ilike', 'product', $this->product])
93             ->andFilterWhere(['ilike', 'modelnumber', $this->modelnumber])
94             ->andFilterWhere(['ilike', 'invent', $this->invent])
95             ->andFilterWhere(['ilike', 'comment', $this->comment]);
96
97         $dataProvider->sort->attributes['statusName'] = [
98             'asc' => [Status::tableName().'.name' => SORT_ASC],
99             'desc' => [Status::tableName().'.name' => SORT_DESC],
100         ];
101         $dataProvider->sort->attributes['typeName'] = [
102             'asc' => [Types::tableName().'.name' => SORT_ASC],
103             'desc' => [Types::tableName().'.name' => SORT_DESC],
104         ];
105         $dataProvider->sort->attributes['locationName'] = [
106             'asc' => [Locations::tableName().'.name' => SORT_ASC],
107             'desc' => [Locations::tableName().'.name' => SORT_DESC],
108         ];
109 //        $dataProvider->sort->attributes['regionName'] = [
110 //            'asc' => [Regions::tableName().'.name' => SORT_ASC],
111 //            'desc' => [Regions::tableName().'.name' => SORT_DESC],
112 //        ];
113         
114         return $dataProvider;
115     }
116 }