Commit 72a1aa2da789ddf3685e2633b4e0e4b4f91d8fe3
1 parent
18fd989c
Catalog optimize
Showing
3 changed files
with
167 additions
and
137 deletions
Show diff stats
models/Category.php
... | ... | @@ -149,7 +149,7 @@ |
149 | 149 | public function getProducts() |
150 | 150 | { |
151 | 151 | return $this->hasMany(Product::className(), [ 'id' => 'product_id' ]) |
152 | - ->viaTable('product_category', [ 'category_id' => 'id' ]); | |
152 | + ->viaTable('product_category', [ 'category_id' => 'id' ])->inverseOf('categories'); | |
153 | 153 | } |
154 | 154 | |
155 | 155 | /** | ... | ... |
models/Product.php
... | ... | @@ -121,7 +121,7 @@ |
121 | 121 | 'product_id' => 'id', |
122 | 122 | ], |
123 | 123 | 'conditions' => [ |
124 | - 'product_variant_id' => null, | |
124 | + 'product_image.product_variant_id' => null, | |
125 | 125 | ], |
126 | 126 | 'model' => ProductImage::className(), |
127 | 127 | 'config' => [ |
... | ... | @@ -365,7 +365,7 @@ |
365 | 365 | public function getCategories() |
366 | 366 | { |
367 | 367 | return $this->hasMany(Category::className(), [ 'id' => 'category_id' ]) |
368 | - ->viaTable('product_category', [ 'product_id' => 'id' ]); | |
368 | + ->viaTable('product_category', [ 'product_id' => 'id' ])->inverseOf('products'); | |
369 | 369 | } |
370 | 370 | |
371 | 371 | /** | ... | ... |
models/ProductFrontendSearch.php
1 | 1 | <?php |
2 | - | |
3 | -namespace artweb\artbox\ecommerce\models; | |
4 | - | |
5 | -use artweb\artbox\ecommerce\helpers\FilterHelper; | |
6 | -use artweb\artbox\ecommerce\models\Category; | |
7 | -use yii\base\Model; | |
8 | -use yii\data\ActiveDataProvider; | |
9 | -use yii\data\ArrayDataProvider; | |
10 | -use yii\db\ActiveQuery; | |
11 | - | |
12 | -use artweb\artbox\ecommerce\models\Product; | |
13 | -use artweb\artbox\ecommerce\models\ProductVariant; | |
14 | - | |
15 | -class ProductFrontendSearch extends Product { | |
16 | - | |
17 | - | |
18 | - public $price_interval; | |
19 | - public $brands; | |
20 | - | |
21 | - public function behaviors() | |
2 | + | |
3 | + namespace artweb\artbox\ecommerce\models; | |
4 | + | |
5 | + use artweb\artbox\ecommerce\helpers\FilterHelper; | |
6 | + use artweb\artbox\ecommerce\models\Category; | |
7 | + use yii\base\Model; | |
8 | + use yii\data\ActiveDataProvider; | |
9 | + use yii\data\ArrayDataProvider; | |
10 | + use yii\db\ActiveQuery; | |
11 | + | |
12 | + use artweb\artbox\ecommerce\models\Product; | |
13 | + use artweb\artbox\ecommerce\models\ProductVariant; | |
14 | + | |
15 | + class ProductFrontendSearch extends Product | |
22 | 16 | { |
23 | - $behaviors = parent::behaviors(); | |
24 | 17 | |
25 | - if (isset($behaviors['language'])) | |
18 | + public $price_interval; | |
19 | + public $brands; | |
20 | + | |
21 | + public function behaviors() | |
26 | 22 | { |
27 | - unset($behaviors['language']); | |
23 | + $behaviors = parent::behaviors(); | |
24 | + | |
25 | + if (isset( $behaviors[ 'language' ] )) { | |
26 | + unset( $behaviors[ 'language' ] ); | |
27 | + } | |
28 | + return $behaviors; | |
28 | 29 | } |
29 | - return $behaviors; | |
30 | - } | |
31 | - | |
32 | - /** | |
33 | - * @inheritdoc | |
34 | - */ | |
35 | - public function rules() | |
36 | - { | |
37 | - return [ | |
38 | - [['price_interval', 'brands'], 'safe'], | |
39 | - ]; | |
40 | - } | |
41 | - | |
42 | - /** | |
43 | - * @inheritdoc | |
44 | - */ | |
45 | - public function scenarios() | |
46 | - { | |
47 | - // bypass scenarios() implementation in the parent class | |
48 | - return Model::scenarios(); | |
49 | - } | |
50 | - | |
51 | - /** | |
52 | - * Creates data provider instance with search query applied for frontend | |
53 | - * | |
54 | - * @param array $params | |
55 | - * | |
56 | - * @return ArrayDataProvider | |
57 | - */ | |
58 | - public function search($category = null, $params = [], $in_stock = true) { | |
59 | - | |
60 | - | |
61 | - $dataProvider = new ArrayDataProvider([ | |
62 | - 'allModels' => $this->getSearchQuery($category, $params, $in_stock)->with([ | |
63 | - 'images', | |
64 | - 'variant', | |
65 | - 'variant.image', | |
66 | - 'comments', | |
67 | -// 'averageRating', | |
68 | - ])->all(), | |
69 | - 'pagination' => [ | |
70 | - 'pageSize' => 15, | |
71 | - ], | |
72 | - 'sort' => [ | |
73 | - 'attributes' => [ | |
74 | - 'name_asc' => [ | |
75 | - 'asc' => ['name' => SORT_ASC], | |
76 | - 'desc' => ['name' => SORT_ASC], | |
77 | - 'default' => SORT_ASC, | |
78 | - 'label' => 'ะธะผะตะฝะธ ะพั ะ ะดะพ ะฏ', | |
79 | - ], | |
80 | - 'name_desc' => [ | |
81 | - 'asc' => ['name' => SORT_DESC], | |
82 | - 'desc' => ['name' => SORT_DESC], | |
83 | - 'default' => SORT_DESC, | |
84 | - 'label' => 'ะธะผะตะฝะธ ะพั ะฏ ะดะพ ะ', | |
85 | - ], | |
86 | - 'price' => [ | |
87 | - 'asc' => ['price' => SORT_ASC], | |
88 | - 'desc' => ['price' => SORT_DESC], | |
89 | - 'default' => SORT_DESC, | |
90 | - 'label' => 'ะฟะพ ัะตะฝะต', | |
30 | + | |
31 | + /** | |
32 | + * @inheritdoc | |
33 | + */ | |
34 | + public function rules() | |
35 | + { | |
36 | + return [ | |
37 | + [ | |
38 | + [ | |
39 | + 'price_interval', | |
40 | + 'brands', | |
91 | 41 | ], |
42 | + 'safe', | |
92 | 43 | ], |
93 | - ] | |
94 | - ]); | |
44 | + ]; | |
45 | + } | |
46 | + | |
47 | + /** | |
48 | + * @inheritdoc | |
49 | + */ | |
50 | + public function scenarios() | |
51 | + { | |
52 | + // bypass scenarios() implementation in the parent class | |
53 | + return Model::scenarios(); | |
54 | + } | |
95 | 55 | |
96 | - | |
97 | - return $dataProvider; | |
98 | - } | |
99 | - | |
100 | - public function getSearchQuery($category = null, $params = [], $in_stock = true) { | |
101 | - | |
102 | - if (!empty($category)) { | |
103 | - /** @var ActiveQuery $query */ | |
104 | - /**@var Category $category **/ | |
105 | - $query = $category->getProducts(); | |
106 | - | |
107 | - } else { | |
108 | - $query = Product::find(); | |
56 | + /** | |
57 | + * Creates data provider instance with search query applied for frontend | |
58 | + * | |
59 | + * @param array $params | |
60 | + * | |
61 | + * @return ArrayDataProvider | |
62 | + */ | |
63 | + public function search($category = null, $params = [], $in_stock = true) | |
64 | + { | |
65 | + | |
66 | + $dataProvider = new ArrayDataProvider( | |
67 | + [ | |
68 | + 'allModels' => $this->getSearchQuery($category, $params, $in_stock) | |
69 | + ->all(), | |
70 | + 'pagination' => [ | |
71 | + 'pageSize' => 10, | |
72 | + ], | |
73 | + 'sort' => [ | |
74 | + 'attributes' => [ | |
75 | + 'name_asc' => [ | |
76 | + 'asc' => [ 'name' => SORT_ASC ], | |
77 | + 'desc' => [ 'name' => SORT_ASC ], | |
78 | + 'default' => SORT_ASC, | |
79 | + 'label' => 'ะธะผะตะฝะธ ะพั ะ ะดะพ ะฏ', | |
80 | + ], | |
81 | + 'name_desc' => [ | |
82 | + 'asc' => [ 'name' => SORT_DESC ], | |
83 | + 'desc' => [ 'name' => SORT_DESC ], | |
84 | + 'default' => SORT_DESC, | |
85 | + 'label' => 'ะธะผะตะฝะธ ะพั ะฏ ะดะพ ะ', | |
86 | + ], | |
87 | + 'price' => [ | |
88 | + 'asc' => [ 'price' => SORT_ASC ], | |
89 | + 'desc' => [ 'price' => SORT_DESC ], | |
90 | + 'default' => SORT_DESC, | |
91 | + 'label' => 'ะฟะพ ัะตะฝะต', | |
92 | + ], | |
93 | + ], | |
94 | + ], | |
95 | + ] | |
96 | + ); | |
97 | + | |
98 | + return $dataProvider; | |
109 | 99 | } |
110 | - | |
111 | - $query->select(['product.*']); | |
112 | - $query->joinWith(['enabledVariants.lang','brand','options', 'category']); | |
113 | - | |
114 | - $query->groupBy(['product.id', 'product_variant.price']); | |
115 | - | |
116 | - FilterHelper::setQueryParams($query, $params); | |
117 | - if($in_stock){ | |
118 | - $query->andWhere(['>=', ProductVariant::tableName() .'.stock', 1]); | |
100 | + | |
101 | + public function getSearchQuery($category = null, $params = [], $in_stock = true) | |
102 | + { | |
103 | + | |
104 | + if (!empty( $category )) { | |
105 | + /** @var ActiveQuery $query */ | |
106 | + /**@var Category $category * */ | |
107 | + $query = $category->getProducts(); | |
108 | + | |
109 | + } else { | |
110 | + $query = Product::find(); | |
111 | + } | |
112 | + | |
113 | + $query->select([ 'product.*' ]); | |
114 | + $query->joinWith( | |
115 | + [ | |
116 | + 'lang', | |
117 | + 'brand.lang', | |
118 | + 'options', | |
119 | + ] | |
120 | + ) | |
121 | + ->innerJoinWith( | |
122 | + [ | |
123 | + 'enabledVariants' => function ($query) { | |
124 | + /** | |
125 | + * @var ActiveQuery $query | |
126 | + */ | |
127 | + $query->joinWith('lang') | |
128 | + ->with('images'); | |
129 | + }, | |
130 | + ] | |
131 | + ); | |
132 | + | |
133 | + $query->groupBy( | |
134 | + [ | |
135 | + 'product.id', | |
136 | + 'product_variant.price', | |
137 | + ] | |
138 | + ); | |
139 | + | |
140 | + FilterHelper::setQueryParams($query, $params); | |
141 | + if ($in_stock) { | |
142 | + $query->andWhere( | |
143 | + [ | |
144 | + '>=', | |
145 | + ProductVariant::tableName() . '.stock', | |
146 | + 1, | |
147 | + ] | |
148 | + ); | |
149 | + } | |
150 | + | |
151 | + return $query; | |
119 | 152 | } |
120 | - | |
121 | - | |
122 | - return $query; | |
123 | - } | |
124 | - | |
125 | - | |
126 | - /** | |
127 | - * @param Category|null $category | |
128 | - * @return array | |
129 | - */ | |
130 | - | |
131 | - public function priceLimits($category = null) { | |
132 | - if (!empty($category)) { | |
133 | - /** @var ActiveQuery $query */ | |
134 | - $query = $category->getProducts(); | |
135 | - } else { | |
136 | - $query = Product::find(); | |
153 | + | |
154 | + /** | |
155 | + * @param Category|null $category | |
156 | + * | |
157 | + * @return array | |
158 | + */ | |
159 | + | |
160 | + public function priceLimits($category = null) | |
161 | + { | |
162 | + if (!empty( $category )) { | |
163 | + /** @var ActiveQuery $query */ | |
164 | + $query = $category->getProducts(); | |
165 | + } else { | |
166 | + $query = Product::find(); | |
167 | + } | |
168 | + $query->joinWith('variant'); | |
169 | + | |
170 | + return [ | |
171 | + 'min' => $query->min(ProductVariant::tableName() . '.price'), | |
172 | + 'max' => $query->max(ProductVariant::tableName() . '.price'), | |
173 | + ]; | |
137 | 174 | } |
138 | - $query->joinWith('variant'); | |
139 | - | |
140 | - return [ | |
141 | - 'min' => $query->min(ProductVariant::tableName() .'.price'), | |
142 | - 'max' => $query->max(ProductVariant::tableName() .'.price'), | |
143 | - ]; | |
144 | - } | |
145 | -} | |
146 | 175 | \ No newline at end of file |
176 | + } | |
147 | 177 | \ No newline at end of file | ... | ... |