6 use yii\data\ActiveDataProvider;
10 use app\models\Locations;
11 use app\models\Regions;
12 use app\models\Moving;
15 * ItemsSearch represents the model behind the search form of `app\models\Items`.
17 class ItemsSearch extends Items
22 public function rules()
25 [['id', 'model_id'], 'integer'],
26 [['name', 'modelName', 'os', 'mac', 'serial', 'invent', 'date', 'comment', 'statusName', 'typeName', 'locationName', 'regionName'], 'safe'],
33 public function scenarios()
35 // bypass scenarios() implementation in the parent class
36 return Model::scenarios();
39 public function noinvent($params)
42 $query = Moving::find()
43 ->select('MAX(' . Moving::tableName() . '.id) AS mid');
44 if (isset($params->region) && ($params->region != ''))
46 $query->joinWith([ 'locations' ])
47 ->where([ 'region_id' => $params->region ]);
49 if (isset($params->location) && ($params->location != ''))
51 if (isset($params->region) && ($params->location != ''))
53 $query->andWhere(['location_id' => $params->location]);
57 $query->where(['location_id' => $params->location]);
60 $query->distinct('item_id')->groupBy('item_id');
62 $query = Items::find()
63 ->select(Items::tableName() . '.*, ' .
64 Locations::tableName() . '.name AS locationName, ' .
65 Types::tableName() . '.name AS typeName, ' .
66 Regions::tableName() . '.name AS regionName, ' .
67 Status::tableName() . '.name AS statusName ')
68 ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models' ])
69 ->where([ 'in', Moving::tableName() . '.id', $query ])
70 ->andWhere([ 'checked' => false ]);
72 $dataProvider = new ActiveDataProvider([
75 $dataProvider->setSort([
80 $dataProvider->sort->attributes['modelName'] = [
81 'asc' => [ Models::tableName() . '.name' => SORT_ASC ],
82 'desc' => [ Models::tableName() . '.name' => SORT_DESC ],
84 $dataProvider->sort->attributes['statusName'] = [
85 'asc' => [ Status::tableName() . '.name' => SORT_ASC ],
86 'desc' => [ Status::tableName() . '.name' => SORT_DESC ],
88 $dataProvider->sort->attributes['typeName'] = [
89 'asc' => [ Types::tableName() . '.name' => SORT_ASC ],
90 'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
92 $dataProvider->sort->attributes['locationName'] = [
93 'asc' => [ Locations::tableName() . '.name' => SORT_ASC ],
94 'desc' => [ Locations::tableName() . '.name' => SORT_DESC ],
96 $dataProvider->sort->attributes['regionName'] = [
97 'asc' => [ Regions::tableName() . '.name' => SORT_ASC ],
98 'desc' => [ Regions::tableName() . '.name' => SORT_DESC ],
100 return $dataProvider;
104 * Creates data provider instance with search query applied
106 * @param array $params
108 * @return ActiveDataProvider
110 public function search($params)
112 // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
113 $subQuery = Moving::find()
114 ->select('MAX(id) AS id')
115 ->distinct('item_id')
116 ->groupBy([ 'item_id' ]);
118 $query = Items::find()
119 ->select(Items::tableName() . '.*, ' .
120 Locations::tableName() . '.name AS locationName, ' .
121 Models::tableName() . '.name AS modelName, ' .
122 Types::tableName() . '.name AS typeName, ' .
123 Regions::tableName() . '.name AS regionName, ' .
124 Status::tableName() . '.name AS statusName ')
125 ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models', ])
126 ->where([ 'in', Moving::tableName() . '.id', $subQuery ]);
128 // add conditions that should always apply here
130 $dataProvider = new ActiveDataProvider([
134 $dataProvider->setSort([
140 $this->load($params);
142 if (!$this->validate())
144 // uncomment the following line if you do not want to return any records when validation fails
145 // $query->where('0=1');
146 return $dataProvider;
149 // grid filtering conditions
150 $query->andFilterWhere([
153 'ilike', Status::tableName() . '.name', $this->statusName
155 'ilike', Models::tableName() . '.name', $this->modelName
157 'ilike', Types::tableName() . '.name', $this->typeName
158 ])->andFilterWhere([ 'OR', [
159 'ilike', Locations::tableName() . '.name', $this->regionName
161 'ilike', Regions::tableName() . '.name', $this->regionName
162 ]])->andFilterWhere([ 'OR', [
163 'ilike', Locations::tableName() . '.name', $this->locationName
165 'ilike', Regions::tableName() . '.name', $this->locationName
168 $query->andFilterWhere(['ilike', 'name', $this->name])
169 ->andFilterWhere( ['ilike', 'os', $this->os])
170 ->andFilterWhere( ['ilike', 'mac', $this->mac])
171 ->andFilterWhere( ['ilike', 'serial', $this->serial])
172 ->andFilterWhere( ['ilike', 'invent', $this->invent])
173 ->andFilterWhere( ['ilike', 'comment', $this->comment]);
175 $dataProvider->sort->attributes['modelName'] = [
176 'asc' => [Models::tableName() . '.name' => SORT_ASC],
177 'desc' => [Models::tableName() . '.name' => SORT_DESC],
179 $dataProvider->sort->attributes['statusName'] = [
180 'asc' => [Status::tableName() . '.name' => SORT_ASC],
181 'desc' => [Status::tableName() . '.name' => SORT_DESC],
183 $dataProvider->sort->attributes['typeName'] = [
184 'asc' => [Types::tableName() . '.name' => SORT_ASC],
185 'desc' => [Types::tableName() . '.name' => SORT_DESC],
187 $dataProvider->sort->attributes['locationName'] = [
188 'asc' => [Locations::tableName() . '.name' => SORT_ASC],
189 'desc' => [Locations::tableName() . '.name' => SORT_DESC],
191 $dataProvider->sort->attributes['regionName'] = [
192 'asc' => [Regions::tableName() . '.name' => SORT_ASC],
193 'desc' => [Regions::tableName() . '.name' => SORT_DESC],
196 return $dataProvider;