@@ -357,65 +357,54 @@ 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
- $ 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
- );
360
+
370
361
foreach ($ attributeTypes as $ backendType => $ attributeIds ) {
371
362
if ($ attributeIds ) {
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 ;
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
+
407
388
}
408
389
}
409
390
410
391
if ($ selects ) {
411
- $ select = $ this ->connection ->select ()->union ($ selects , Select::SQL_UNION_ALL );
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 );
412
402
$ query = $ this ->connection ->query ($ select );
413
403
while ($ row = $ query ->fetch ()) {
414
- $ entityId = $ productLinkFieldsToEntityIdMap [ $ row [$ linkField ] ];
404
+ $ entityId = $ row [' entity_id ' ];
415
405
$ result [$ entityId ][$ row ['attribute_id ' ]] = $ row ['value ' ];
416
406
}
417
407
}
418
-
419
408
return $ result ;
420
409
}
421
410
0 commit comments