25
25
use Magento \Store \Model \Indexer \WebsiteDimensionProvider ;
26
26
use Magento \Store \Model \Store ;
27
27
use Magento \Catalog \Model \ResourceModel \Category ;
28
+ use Zend_Db_Expr ;
29
+ use Magento \Catalog \Model \ResourceModel \Product \Gallery ;
28
30
29
31
/**
30
32
* Product collection
@@ -43,70 +45,54 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
43
45
/**
44
46
* Alias for index table
45
47
*/
46
- const INDEX_TABLE_ALIAS = 'price_index ' ;
48
+ public const INDEX_TABLE_ALIAS = 'price_index ' ;
47
49
48
50
/**
49
51
* Alias for main table
50
52
*/
51
- const MAIN_TABLE_ALIAS = 'e ' ;
53
+ public const MAIN_TABLE_ALIAS = 'e ' ;
52
54
53
55
/**
54
56
* @var string
55
57
*/
56
58
protected $ _idFieldName = 'entity_id ' ;
57
59
58
60
/**
59
- * Catalog Product Flat is enabled cache per store
60
- *
61
61
* @var array
62
62
*/
63
63
protected $ _flatEnabled = [];
64
64
65
65
/**
66
- * Product websites table name
67
- *
68
66
* @var string
69
67
*/
70
68
protected $ _productWebsiteTable ;
71
69
72
70
/**
73
- * Product categories table name
74
- *
75
71
* @var string
76
72
*/
77
73
protected $ _productCategoryTable ;
78
74
79
75
/**
80
- * Is add URL rewrites to collection flag
81
- *
82
76
* @var bool
83
77
*/
84
78
protected $ _addUrlRewrite = false ;
85
79
86
80
/**
87
- * Add URL rewrite for category
88
- *
89
81
* @var int
90
82
*/
91
83
protected $ _urlRewriteCategory = '' ;
92
84
93
85
/**
94
- * Is add final price to product collection flag
95
- *
96
86
* @var bool
97
87
*/
98
88
protected $ _addFinalPrice = false ;
99
89
100
90
/**
101
- * Cache for all ids
102
- *
103
91
* @var array
104
92
*/
105
93
protected $ _allIdsCache = null ;
106
94
107
95
/**
108
- * Is add tax percents to product collection flag
109
- *
110
96
* @var bool
111
97
*/
112
98
protected $ _addTaxPercents = false ;
@@ -136,43 +122,31 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
136
122
protected $ _priceDataFieldFilters = [];
137
123
138
124
/**
139
- * Price expression sql
140
- *
141
125
* @var string|null
142
126
*/
143
127
protected $ _priceExpression ;
144
128
145
129
/**
146
- * Additional price expression sql part
147
- *
148
130
* @var string|null
149
131
*/
150
132
protected $ _additionalPriceExpression ;
151
133
152
134
/**
153
- * Max prise (statistics data)
154
- *
155
135
* @var float
156
136
*/
157
137
protected $ _maxPrice ;
158
138
159
139
/**
160
- * Min prise (statistics data)
161
- *
162
140
* @var float
163
141
*/
164
142
protected $ _minPrice ;
165
143
166
144
/**
167
- * Prise standard deviation (statistics data)
168
- *
169
145
* @var float
170
146
*/
171
147
protected $ _priceStandardDeviation ;
172
148
173
149
/**
174
- * Prises count (statistics data)
175
- *
176
150
* @var int
177
151
*/
178
152
protected $ _pricesCount = null ;
@@ -206,8 +180,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
206
180
protected $ _scopeConfig ;
207
181
208
182
/**
209
- * Customer session
210
- *
211
183
* @var \Magento\Customer\Model\Session
212
184
*/
213
185
protected $ _customerSession ;
@@ -218,15 +190,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
218
190
protected $ _localeDate ;
219
191
220
192
/**
221
- * Catalog url
222
- *
223
193
* @var \Magento\Catalog\Model\ResourceModel\Url
224
194
*/
225
195
protected $ _catalogUrl ;
226
196
227
197
/**
228
- * Product option factory
229
- *
230
198
* @var \Magento\Catalog\Model\Product\OptionFactory
231
199
*/
232
200
protected $ _productOptionFactory ;
@@ -249,8 +217,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
249
217
protected $ _groupManagement ;
250
218
251
219
/**
252
- * Need to add websites to result flag
253
- *
254
220
* @var bool
255
221
*/
256
222
protected $ needToAddWebsiteNamesToResult ;
@@ -339,8 +305,13 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
339
305
* @param PriceTableResolver|null $priceTableResolver
340
306
* @param DimensionFactory|null $dimensionFactory
341
307
* @param Category|null $categoryResourceModel
308
+ * @param DbStorage|null $urlFinder
309
+ * @param GalleryReadHandler|null $productGalleryReadHandler
310
+ * @param Gallery|null $mediaGalleryResource
342
311
*
343
312
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
313
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
314
+ * @SuppressWarnings(PHPMD.NPathComplexity)
344
315
*/
345
316
public function __construct (
346
317
\Magento \Framework \Data \Collection \EntityFactory $ entityFactory ,
@@ -368,7 +339,10 @@ public function __construct(
368
339
TableMaintainer $ tableMaintainer = null ,
369
340
PriceTableResolver $ priceTableResolver = null ,
370
341
DimensionFactory $ dimensionFactory = null ,
371
- Category $ categoryResourceModel = null
342
+ Category $ categoryResourceModel = null ,
343
+ DbStorage $ urlFinder = null ,
344
+ GalleryReadHandler $ productGalleryReadHandler = null ,
345
+ Gallery $ mediaGalleryResource = null
372
346
) {
373
347
$ this ->moduleManager = $ moduleManager ;
374
348
$ this ->_catalogProductFlatState = $ catalogProductFlatState ;
@@ -398,25 +372,19 @@ public function __construct(
398
372
$ storeManager ,
399
373
$ connection
400
374
);
401
- $ this ->tableMaintainer = $ tableMaintainer ?: ObjectManager::getInstance ()->get (TableMaintainer::class);
402
- $ this ->priceTableResolver = $ priceTableResolver ?: ObjectManager::getInstance ()->get (PriceTableResolver::class);
375
+ $ this ->tableMaintainer = $ tableMaintainer ?: ObjectManager::getInstance ()
376
+ ->get (TableMaintainer::class);
377
+ $ this ->priceTableResolver = $ priceTableResolver ?: ObjectManager::getInstance ()
378
+ ->get (PriceTableResolver::class);
403
379
$ this ->dimensionFactory = $ dimensionFactory
404
380
?: ObjectManager::getInstance ()->get (DimensionFactory::class);
405
381
$ this ->categoryResourceModel = $ categoryResourceModel ?: ObjectManager::getInstance ()
406
382
->get (Category::class);
407
- }
408
-
409
- /**
410
- * Retrieve urlFinder
411
- *
412
- * @return GalleryReadHandler
413
- */
414
- private function getUrlFinder ()
415
- {
416
- if ($ this ->urlFinder === null ) {
417
- $ this ->urlFinder = ObjectManager::getInstance ()->get (DbStorage::class);
418
- }
419
- return $ this ->urlFinder ;
383
+ $ this ->urlFinder = $ urlFinder ?: ObjectManager::getInstance ()->get (DbStorage::class);
384
+ $ this ->productGalleryReadHandler = $ productGalleryReadHandler ?: ObjectManager::getInstance ()
385
+ ->get (GalleryReadHandler::class);
386
+ $ this ->mediaGalleryResource = $ mediaGalleryResource ?: ObjectManager::getInstance ()
387
+ ->get (Gallery::class);
420
388
}
421
389
422
390
/**
@@ -643,7 +611,7 @@ protected function _initSelect()
643
611
[self ::MAIN_TABLE_ALIAS => $ this ->getEntity ()->getFlatTableName ()],
644
612
null
645
613
)->columns (
646
- ['status ' => new \ Zend_Db_Expr (ProductStatus::STATUS_ENABLED )]
614
+ ['status ' => new Zend_Db_Expr (ProductStatus::STATUS_ENABLED )]
647
615
);
648
616
$ this ->addAttributeToSelect ($ this ->getResource ()->getDefaultAttributes ());
649
617
if ($ this ->_catalogProductFlatState ->getFlatIndexerHelper ()->isAddChildData ()) {
@@ -791,7 +759,7 @@ public function addIdFilter($productId, $exclude = false)
791
759
return $ this ;
792
760
}
793
761
if (is_array ($ productId )) {
794
- if (! empty ( $ productId) ) {
762
+ if ($ productId ) {
795
763
if ($ exclude ) {
796
764
$ condition = ['nin ' => $ productId ];
797
765
} else {
@@ -1028,7 +996,7 @@ public function getMaxAttributeValue($attribute)
1028
996
$ select ->join (
1029
997
[$ tableAlias => $ attribute ->getBackend ()->getTable ()],
1030
998
$ condition ,
1031
- [$ fieldAlias => new \ Zend_Db_Expr ('MAX( ' . $ tableAlias . '.value) ' )]
999
+ [$ fieldAlias => new Zend_Db_Expr ('MAX( ' . $ tableAlias . '.value) ' )]
1032
1000
)->group (
1033
1001
'e.entity_type_id '
1034
1002
);
@@ -1065,8 +1033,8 @@ public function getAttributeValueCountByRange($attribute, $range)
1065
1033
[$ tableAlias => $ attribute ->getBackend ()->getTable ()],
1066
1034
$ condition ,
1067
1035
[
1068
- 'count_ ' . $ attributeCode => new \ Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1069
- 'range_ ' . $ attributeCode => new \ Zend_Db_Expr ('CEIL(( ' . $ tableAlias . '.value+0.01)/ ' . $ range . ') ' )
1036
+ 'count_ ' . $ attributeCode => new Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1037
+ 'range_ ' . $ attributeCode => new Zend_Db_Expr ('CEIL(( ' . $ tableAlias . '.value+0.01)/ ' . $ range . ') ' )
1070
1038
]
1071
1039
)->group (
1072
1040
'range_ ' . $ attributeCode
@@ -1104,8 +1072,8 @@ public function getAttributeValueCount($attribute)
1104
1072
[$ tableAlias => $ attribute ->getBackend ()->getTable ()],
1105
1073
$ condition ,
1106
1074
[
1107
- 'count_ ' . $ attributeCode => new \ Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1108
- 'value_ ' . $ attributeCode => new \ Zend_Db_Expr ($ tableAlias . '.value ' )
1075
+ 'count_ ' . $ attributeCode => new Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1076
+ 'value_ ' . $ attributeCode => new Zend_Db_Expr ($ tableAlias . '.value ' )
1109
1077
]
1110
1078
)->group (
1111
1079
'value_ ' . $ attributeCode
@@ -1311,7 +1279,7 @@ public function getProductCountSelect()
1311
1279
'count_table.product_id = e.entity_id ' ,
1312
1280
[
1313
1281
'count_table.category_id ' ,
1314
- 'product_count ' => new \ Zend_Db_Expr ('COUNT(DISTINCT count_table.product_id) ' )
1282
+ 'product_count ' => new Zend_Db_Expr ('COUNT(DISTINCT count_table.product_id) ' )
1315
1283
]
1316
1284
)->where (
1317
1285
'count_table.store_id = ? ' ,
@@ -1490,7 +1458,7 @@ protected function _addUrlRewrite()
1490
1458
$ filter ['metadata ' ]['category_id ' ] = $ this ->_urlRewriteCategory ;
1491
1459
}
1492
1460
1493
- $ rewrites = $ this ->getUrlFinder () ->findAllByData ($ filter );
1461
+ $ rewrites = $ this ->urlFinder ->findAllByData ($ filter );
1494
1462
foreach ($ rewrites as $ rewrite ) {
1495
1463
if ($ item = $ this ->getItemById ($ rewrite ->getEntityId ())) {
1496
1464
$ item ->setData ('request_path ' , $ rewrite ->getRequestPath ());
@@ -2090,28 +2058,25 @@ protected function _applyZeroStoreProductLimitations()
2090
2058
{
2091
2059
$ filters = $ this ->_productLimitationFilters ;
2092
2060
$ categories = $ this ->getChildrenCategories ((int )$ filters ['category_id ' ]);
2061
+ $ joinCond = 'cat_pro.product_id = e.entity_id ' ;
2093
2062
2094
- $ conditions = [
2095
- 'cat_pro.product_id=e.entity_id ' ,
2096
- $ this ->getConnection ()->quoteInto (
2097
- 'cat_pro.category_id IN (?) ' ,
2098
- $ categories
2099
- ),
2100
- ];
2101
- $ joinCond = join (' AND ' , $ conditions );
2102
-
2103
- $ fromPart = $ this ->getSelect ()->getPart (\Magento \Framework \DB \Select::FROM );
2063
+ $ fromPart = $ this ->getSelect ()->getPart (Select::FROM );
2104
2064
if (isset ($ fromPart ['cat_pro ' ])) {
2105
2065
$ fromPart ['cat_pro ' ]['joinCondition ' ] = $ joinCond ;
2106
- $ this ->getSelect ()->setPart (\ Magento \ Framework \ DB \ Select::FROM , $ fromPart );
2066
+ $ this ->getSelect ()->setPart (Select::FROM , $ fromPart );
2107
2067
} else {
2068
+ $ conditions = [
2069
+ $ joinCond ,
2070
+ $ this ->getConnection ()->quoteInto ('cat_pro.category_id IN(?) ' , $ categories , 'int ' ),
2071
+ ];
2072
+ $ joinCond = join (' AND ' , $ conditions );
2108
2073
$ this ->getSelect ()->join (
2109
2074
['cat_pro ' => $ this ->getTable ('catalog_category_product ' )],
2110
2075
$ joinCond ,
2111
- ['cat_index_position ' => ' position ' ]
2112
- );
2076
+ ['cat_index_position ' => new Zend_Db_Expr ( ' MIN(cat_pro. position) ' ) ]
2077
+ )-> group ( ' e.entity_id ' ) ;
2113
2078
}
2114
- $ this ->_joinFields ['position ' ] = ['table ' => 'cat_pro ' , 'field ' => 'position ' ];
2079
+ $ this ->_joinFields ['position ' ] = ['table ' => 'cat_pro ' , 'field ' => 'min_position ' ];
2115
2080
2116
2081
return $ this ;
2117
2082
}
@@ -2370,7 +2335,7 @@ public function addMediaGalleryData()
2370
2335
$ items = $ this ->getItems ();
2371
2336
$ linkField = $ this ->getProductEntityMetadata ()->getLinkField ();
2372
2337
2373
- $ select = $ this ->getMediaGalleryResource ()
2338
+ $ select = $ this ->mediaGalleryResource
2374
2339
->createBatchBaseSelect (
2375
2340
$ this ->getStoreId (),
2376
2341
$ this ->getAttribute ('media_gallery ' )->getAttributeId ()
@@ -2392,7 +2357,7 @@ function ($item) use ($linkField) {
2392
2357
}
2393
2358
2394
2359
foreach ($ items as $ item ) {
2395
- $ this ->getGalleryReadHandler ()
2360
+ $ this ->productGalleryReadHandler
2396
2361
->addMediaDataToProduct (
2397
2362
$ item ,
2398
2363
$ mediaGalleries [$ item ->getOrigData ($ linkField )] ?? []
@@ -2414,35 +2379,6 @@ public function getProductEntityMetadata()
2414
2379
return $ this ->metadataPool ->getMetadata (ProductInterface::class);
2415
2380
}
2416
2381
2417
- /**
2418
- * Retrieve GalleryReadHandler
2419
- *
2420
- * @return GalleryReadHandler
2421
- * @deprecated 101.0.1
2422
- */
2423
- private function getGalleryReadHandler ()
2424
- {
2425
- if ($ this ->productGalleryReadHandler === null ) {
2426
- $ this ->productGalleryReadHandler = ObjectManager::getInstance ()->get (GalleryReadHandler::class);
2427
- }
2428
- return $ this ->productGalleryReadHandler ;
2429
- }
2430
-
2431
- /**
2432
- * Retrieve Media gallery resource.
2433
- *
2434
- * @deprecated 101.0.1
2435
- *
2436
- * @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
2437
- */
2438
- private function getMediaGalleryResource ()
2439
- {
2440
- if (null === $ this ->mediaGalleryResource ) {
2441
- $ this ->mediaGalleryResource = ObjectManager::getInstance ()->get (Gallery::class);
2442
- }
2443
- return $ this ->mediaGalleryResource ;
2444
- }
2445
-
2446
2382
/**
2447
2383
* Clear collection
2448
2384
*
@@ -2551,7 +2487,7 @@ private function addIsSaleableAttributeToFilter($condition): self
2551
2487
foreach ($ columns as $ columnEntry ) {
2552
2488
list ($ correlationName , $ column , $ alias ) = $ columnEntry ;
2553
2489
if ($ alias == 'is_saleable ' ) {
2554
- if ($ column instanceof \ Zend_Db_Expr) {
2490
+ if ($ column instanceof Zend_Db_Expr) {
2555
2491
$ field = $ column ;
2556
2492
} else {
2557
2493
$ connection = $ this ->getSelect ()->getConnection ();
0 commit comments