Blame view

models/ProductSearch.php 6.03 KB
2f25da09   Yarik   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  <?php

      

      namespace artweb\artbox\ecommerce\models;

      

      use yii\base\Model;

      use yii\data\ActiveDataProvider;

      use yii\db\ActiveQuery;

      

      /**

       * ProductSearch represents the model behind the search form about

       * `artweb\artbox\ecommerce\models\Product`.

       */

      class ProductSearch extends Product

      {

          

          public $categoryId;

          

          public $productName;

998b4c25   Administrator   add variantSku
19
          public $variantSku;

2f25da09   Yarik   first commit
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
          public $variantCount;

          

          public function behaviors()

          {

              $behaviors = parent::behaviors();

              if (isset( $behaviors[ 'language' ] )) {

                  unset( $behaviors[ 'language' ] );

              }

              return $behaviors;

          }

          

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [

                          'productName',

998b4c25   Administrator   add variantSku
40
                          'variantSku'

2f25da09   Yarik   first commit
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                      ],

                      'safe',

                  ],

                  [

                      [

                          'brand_id',

                          'id',

                          'categoryId',

                      ],

                      'integer',

                  ],

                  [

                      [

                          'is_top',

                          'is_new',

                          'is_discount',

                      ],

                      'boolean',

                  ],

              ];

          }

          

          public function attributeLabels()

          {

              $labels = parent::attributeLabels();

              $new_labels = [

                  'categoryId'   => 'Category ID',

                  'brand_id'     => 'Brand ID',

                  'productName'  => 'Product name',

                  'variantCount' => 'Variant count',

              ];

              return array_merge($labels, $new_labels);

          }

          

          /**

           * @inheritdoc

           */

          public function scenarios()

          {

              // bypass scenarios() implementation in the parent class

              return Model::scenarios();

          }

          

          /**

           * Creates data provider instance with search query applied

           *

           * @param array $params

           *

           * @return ActiveDataProvider

           */

          public function search($params)

          {

              $query = Product::find();

              $query->select(

                  [

                      'product.*',

                  ]

              );

              

              $query->joinWith(

                  [

                      'categories',

                      'lang',

                  ]

fa33d79b   Yarik   Namespaces
105
106
107
108
109
              )//                  ->joinWith(

                  //                      [

                  //                          'brand.lang',

                  //                      ]

                  //                  )

2f25da09   Yarik   first commit
110
111
112
                                /**

                                 * @var ActiveQuery $query

                                 */

2f25da09   Yarik   first commit
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
                    ->joinWith('variants');

              

              $query->groupBy(

                  [

                      'product.id',

                      'product_variant.id',

                  ]

              );

              

              $dataProvider = new ActiveDataProvider(

                  [

                      'query' => $query,

                  ]

              );

              

              $dataProvider->setSort(

                  [

                      'attributes' => [

                          'id',

                          'productName'  => [

                              'asc'  => [ 'product_lang.title' => SORT_ASC ],

                              'desc' => [ 'product_lang.title' => SORT_DESC ],

                          ],

998b4c25   Administrator   add variantSku
136
                          'variantSku',

fa33d79b   Yarik   Namespaces
137
138
139
140
141
                          //                        'brand_id'     => [

                          //                            'asc'     => [ 'brand_lang.title' => SORT_ASC ],

                          //                            'desc'    => [ 'brand_lang.title' => SORT_DESC ],

                          //                            'default' => SORT_DESC,

                          //                        ],

2f25da09   Yarik   first commit
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                      ],

                  ]

              );

              

              $this->load($params);

              

              if (!$this->validate()) {

                  // uncomment the following line if you do not want to return any records when validation fails

                  // $query->where('0=1');

                  return $dataProvider;

              }

              

              if (isset( $this->is_top )) {

                  $query->andWhere(

                      [

                          'is_top' => (bool) $this->is_top,

                      ]

                  );

              }

              if (isset( $this->is_new )) {

                  $query->andWhere(

                      [

                          'is_new' => (bool) $this->is_new,

                      ]

                  );

              }

              if (isset( $this->is_discount )) {

                  $query->andWhere(

                      [

                          'is_discount' => (bool) $this->is_discount,

                      ]

                  );

              }

              $query->andFilterWhere(

                  [

fa33d79b   Yarik   Namespaces
177
                      //                    'product.brand_id'             => $this->brand_id,

2f25da09   Yarik   first commit
178
179
180
181
182
183
184
185
186
187
188
                      'product.id'                   => $this->id,

                      'product_category.category_id' => $this->categoryId,

                  ]

              );

              $query->andFilterWhere(

                  [

                      'like',

                      'product_lang.title',

                      $this->productName,

                  ]

              );

998b4c25   Administrator   add variantSku
189
190
191
192
193
194
195
              $query->andFilterWhere(

                  [

                      'ilike',

                      'product_variant.sku',

                      $this->variantSku

                  ]

              );

2f25da09   Yarik   first commit
196
197
198
199
              

              return $dataProvider;

          }

      }