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', 'type_id'], 'integer'],
26 [['name', 'model', 'os', 'mac', 'serial', 'product', 'modelnumber', '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' ])
69 ->where([ 'in', Moving::tableName() . '.id', $query ])
70 ->andWhere([ 'checked' => false ]);
72 $dataProvider = new ActiveDataProvider([
75 $dataProvider->setSort([
80 $dataProvider->sort->attributes['statusName'] = [
81 'asc' => [Status::tableName() . '.name' => SORT_ASC],
82 'desc' => [Status::tableName() . '.name' => SORT_DESC],
84 $dataProvider->sort->attributes['typeName'] = [
85 'asc' => [Types::tableName() . '.name' => SORT_ASC],
86 'desc' => [Types::tableName() . '.name' => SORT_DESC],
88 $dataProvider->sort->attributes['locationName'] = [
89 'asc' => [Locations::tableName() . '.name' => SORT_ASC],
90 'desc' => [Locations::tableName() . '.name' => SORT_DESC],
92 $dataProvider->sort->attributes['regionName'] = [
93 'asc' => [Regions::tableName() . '.name' => SORT_ASC],
94 'desc' => [Regions::tableName() . '.name' => SORT_DESC],
100 * Creates data provider instance with search query applied
102 * @param array $params
104 * @return ActiveDataProvider
106 public function search($params)
108 // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
109 $subQuery = Moving::find()
110 ->select('MAX(id) AS id')
111 ->distinct('item_id')
112 ->groupBy([ 'item_id' ]);
114 $query = Items::find()
115 ->select(Items::tableName() . '.*, ' .
116 Locations::tableName() . '.name AS locationName, ' .
117 Types::tableName() . '.name AS typeName, ' .
118 Regions::tableName() . '.name AS regionName, ' .
119 Status::tableName() . '.name AS statusName ')
120 ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions' ])
121 ->where([ 'in', Moving::tableName() . '.id', $subQuery ]);
123 // add conditions that should always apply here
125 $dataProvider = new ActiveDataProvider([
129 $dataProvider->setSort([
135 $this->load($params);
137 if (!$this->validate())
139 // uncomment the following line if you do not want to return any records when validation fails
140 // $query->where('0=1');
141 return $dataProvider;
144 // grid filtering conditions
145 $query->andFilterWhere([
148 'ilike', Status::tableName() . '.name', $this->statusName
150 'ilike', Types::tableName() . '.name', $this->typeName
151 ])->andFilterWhere([ 'OR', [
152 'ilike', Locations::tableName() . '.name', $this->regionName
154 'ilike', Regions::tableName() . '.name', $this->regionName
155 ]])->andFilterWhere([ 'OR', [
156 'ilike', Locations::tableName() . '.name', $this->locationName
158 'ilike', Regions::tableName() . '.name', $this->locationName
161 $query->andFilterWhere(['ilike', 'name', $this->name])
162 ->andFilterWhere( ['ilike', 'model', $this->model])
163 ->andFilterWhere( ['ilike', 'os', $this->os])
164 ->andFilterWhere( ['ilike', 'mac', $this->mac])
165 ->andFilterWhere( ['ilike', 'serial', $this->serial])
166 ->andFilterWhere( ['ilike', 'product', $this->product])
167 ->andFilterWhere( ['ilike', 'modelnumber', $this->modelnumber])
168 ->andFilterWhere( ['ilike', 'invent', $this->invent])
169 ->andFilterWhere( ['ilike', 'comment', $this->comment]);
171 $dataProvider->sort->attributes['statusName'] = [
172 'asc' => [Status::tableName() . '.name' => SORT_ASC],
173 'desc' => [Status::tableName() . '.name' => SORT_DESC],
175 $dataProvider->sort->attributes['typeName'] = [
176 'asc' => [Types::tableName() . '.name' => SORT_ASC],
177 'desc' => [Types::tableName() . '.name' => SORT_DESC],
179 $dataProvider->sort->attributes['locationName'] = [
180 'asc' => [Locations::tableName() . '.name' => SORT_ASC],
181 'desc' => [Locations::tableName() . '.name' => SORT_DESC],
183 $dataProvider->sort->attributes['regionName'] = [
184 'asc' => [Regions::tableName() . '.name' => SORT_ASC],
185 'desc' => [Regions::tableName() . '.name' => SORT_DESC],
188 return $dataProvider;