OSDN Git Service

Исправлен фильтр в моделях по наименованию.
[invent/invent.git] / models / ModelsSearch.php
1 <?php
2
3 namespace app\models;
4
5 use yii\base\Model;
6 use yii\data\ActiveDataProvider;
7 use app\models\Models;
8 use app\models\Types;
9
10 /**
11  * ModelsSearch represents the model behind the search form of `app\models\Models`.
12  */
13 class ModelsSearch extends Models
14 {
15     /**
16      * {@inheritdoc}
17      */
18     public function rules()
19     {
20         return [
21             [['id', 'type_id'], 'integer'],
22             [['name', 'modelnumber', 'product', 'typeName'], 'safe'],
23         ];
24     }
25
26     /**
27      * {@inheritdoc}
28      */
29     public function scenarios()
30     {
31         // bypass scenarios() implementation in the parent class
32         return Model::scenarios();
33     }
34
35     /**
36      * Creates data provider instance with search query applied
37      *
38      * @param array $params
39      *
40      * @return ActiveDataProvider
41      */
42     public function search($params)
43     {
44         $query = Models::find()
45             ->select(Models::tableName().'.*, ' . Types::tableName() . '.name AS typeName')
46             ->joinWith([ 'types' ]);
47
48         // add conditions that should always apply here
49
50         $dataProvider = new ActiveDataProvider([
51             'query' => $query,
52         ]);
53
54         $dataProvider->setSort([
55             'defaultOrder' => [
56                 'name' => SORT_ASC,
57             ],
58         ]);
59         
60         $dataProvider->sort->attributes['typeName'] = [
61             'asc'  => [ Types::tableName() . '.name' => SORT_ASC ],
62             'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
63         ];
64
65
66         $this->load($params);
67
68         if (!$this->validate()) {
69             // uncomment the following line if you do not want to return any records when validation fails
70             // $query->where('0=1');
71             return $dataProvider;
72         }
73
74         // grid filtering conditions
75         $query->andFilterWhere([
76             'id' => $this->id,
77             'type_id' => $this->type_id,
78         ]);
79
80         $query->andFilterWhere([ 'ilike', Models::tablename() . '.name', $this->name ])
81             ->andFilterWhere([ 'ilike', 'modelnumber', $this->modelnumber ])
82             ->andFilterWhere([ 'ilike', 'product', $this->product ])
83             ->andFilterWhere([ 'ilike', Types::tableName() . '.name', $this->typeName ]);
84
85         return $dataProvider;
86     }
87 }