@@ -357,54 +357,65 @@ public function getProductAttributes($storeId, array $productIds, array $attribu
357
357
$ selects = [];
358
358
$ ifStoreValue = $ this ->connection ->getCheckSql ('t_store.value_id > 0 ' , 't_store.value ' , 't_default.value ' );
359
359
$ linkField = $ this ->metadata ->getLinkField ();
360
-
360
+ $ productLinkFieldsToEntityIdMap = $ this ->connection ->fetchPairs (
361
+ $ this ->connection ->select ()->from (
362
+ ['cpe ' => $ this ->getTable ('catalog_product_entity ' )],
363
+ [$ linkField , 'entity_id ' ]
364
+ )->where (
365
+ 'cpe.entity_id IN (?) ' ,
366
+ $ productIds ,
367
+ Zend_Db::INT_TYPE
368
+ )
369
+ );
361
370
foreach ($ attributeTypes as $ backendType => $ attributeIds ) {
362
371
if ($ attributeIds ) {
363
- foreach ($ attributeIds as $ attributeId ) {
364
- $ tableName = $ this ->getTable ('catalog_product_entity_ ' . $ backendType );
365
- $ select = $ this ->connection ->select ()->from (
366
- 't ' ,
367
- [
368
- 'entity_id ' => 't.entity_id ' ,
369
- 'value ' => $ this ->unifyField ($ ifStoreValue , $ backendType ),
370
- ]
371
- )->joinLeft (
372
- ['t_default ' => $ tableName ],
373
- 't. ' . $ linkField . ' = t_default. ' . $ linkField
374
- . ' AND t_default.attribute_id = ' . (int ) $ attributeId
375
- . ' AND t_default.store_id = 0 ' ,
376
- ['attribute_id ' => 't_default.attribute_id ' ]
377
- )->joinLeft (
378
- ['t_store ' => $ tableName ],
379
- 't. ' . $ linkField . ' = t_store. ' . $ linkField
380
- . ' AND t_store.attribute_id = ' . (int ) $ attributeId
381
- . ' AND t_store.store_id = ' . (int ) $ storeId ,
382
-
383
- []
384
- );
385
- $ selects [] = $ select ;
386
- }
387
-
372
+ $ tableName = $ this ->getTable ('catalog_product_entity_ ' . $ backendType );
373
+
374
+ $ select = $ this ->connection ->select ()->from (
375
+ ['t ' => $ tableName ],
376
+ [
377
+ $ linkField => 't. ' . $ linkField ,
378
+ 'attribute_id ' => 't.attribute_id ' ,
379
+ 'value ' => $ this ->unifyField ($ ifStoreValue , $ backendType ),
380
+ ]
381
+ )->joinLeft (
382
+ ['t_store ' => $ tableName ],
383
+ $ this ->connection ->quoteInto (
384
+ 't. ' . $ linkField . '=t_store. ' . $ linkField .
385
+ ' AND t.attribute_id=t_store.attribute_id ' .
386
+ ' AND t_store.store_id = ? ' ,
387
+ $ storeId
388
+ ),
389
+ []
390
+ )->joinLeft (
391
+ ['t_default ' => $ tableName ],
392
+ $ this ->connection ->quoteInto (
393
+ 't. ' . $ linkField . '=t_default. ' . $ linkField .
394
+ ' AND t.attribute_id=t_default.attribute_id ' .
395
+ ' AND t_default.store_id = ? ' ,
396
+ 0
397
+ ),
398
+ []
399
+ )->where (
400
+ 't.attribute_id IN (?) ' ,
401
+ $ attributeIds
402
+ )->where (
403
+ 't. ' . $ linkField . ' IN (?) ' ,
404
+ array_keys ($ productLinkFieldsToEntityIdMap )
405
+ )->distinct ();
406
+ $ selects [] = $ select ;
388
407
}
389
408
}
390
409
391
410
if ($ selects ) {
392
- $ select = 'WITH t AS ( ' .
393
- $ this ->connection ->select ()->from (
394
- ['cpe ' => $ this ->getTable ('catalog_product_entity ' )],
395
- $ linkField != 'entity_id ' ? [$ linkField , 'entity_id ' ] : ['entity_id ' ]
396
- )->where (
397
- 'cpe.entity_id IN (?) ' ,
398
- $ productIds ,
399
- Zend_Db::INT_TYPE
400
- ) . ') ' .
401
- $ this ->connection ->select ()->union ($ selects , Select::SQL_UNION_ALL );
411
+ $ select = $ this ->connection ->select ()->union ($ selects , Select::SQL_UNION_ALL );
402
412
$ query = $ this ->connection ->query ($ select );
403
413
while ($ row = $ query ->fetch ()) {
404
- $ entityId = $ row [' entity_id ' ];
414
+ $ entityId = $ productLinkFieldsToEntityIdMap [ $ row [$ linkField ] ];
405
415
$ result [$ entityId ][$ row ['attribute_id ' ]] = $ row ['value ' ];
406
416
}
407
417
}
418
+
408
419
return $ result ;
409
420
}
410
421
0 commit comments