OSDN Git Service

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