Skip to content

Commit 3a54083

Browse files
committed
read image metadata from database first
1 parent e2854db commit 3a54083

File tree

3 files changed

+16
-38
lines changed

3 files changed

+16
-38
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ public function getAttribute()
245245
'media_gallery'
246246
);
247247
}
248-
249248
return $this->attribute;
250249
}
251250

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,49 +2334,32 @@ public function addPriceDataFieldFilter($comparisonFormat, $fields)
23342334
* @SuppressWarnings(PHPMD.NPathComplexity)
23352335
* @since 101.0.1
23362336
* @throws \Magento\Framework\Exception\LocalizedException
2337+
* @throws \Zend_Db_Statement_Exception
23372338
*/
23382339
public function addMediaGalleryData()
23392340
{
23402341
if ($this->getFlag('media_gallery_added')) {
23412342
return $this;
23422343
}
2343-
23442344
if (!$this->getSize()) {
23452345
return $this;
23462346
}
2347-
2348-
$items = $this->getItems();
2349-
$linkField = $this->getProductEntityMetadata()->getLinkField();
2350-
2351-
$select = $this->getMediaGalleryResource()
2352-
->createBatchBaseSelect(
2353-
$this->getStoreId(),
2354-
$this->getAttribute('media_gallery')->getAttributeId()
2355-
)->reset(
2356-
Select::ORDER // we don't care what order is in current scenario
2357-
)->where(
2358-
'entity.' . $linkField . ' IN (?)',
2359-
array_map(
2360-
function ($item) use ($linkField) {
2361-
return (int) $item->getOrigData($linkField);
2362-
},
2363-
$items
2364-
)
2365-
);
2366-
2347+
$records = $this->getMediaGalleryResource()->getMediaRecords(
2348+
$this->getStoreId(),
2349+
$this->getLoadedIds()
2350+
);
23672351
$mediaGalleries = [];
2368-
foreach ($this->getConnection()->fetchAll($select) as $row) {
2369-
$mediaGalleries[$row[$linkField]][] = $row;
2352+
foreach ($records as $record) {
2353+
$mediaGalleries[$record['entity_id']][] = $record;
23702354
}
23712355

2372-
foreach ($items as $item) {
2356+
foreach ($this->getItems() as $item) {
23732357
$this->getGalleryReadHandler()
23742358
->addMediaDataToProduct(
23752359
$item,
2376-
$mediaGalleries[$item->getOrigData($linkField)] ?? []
2360+
$mediaGalleries[$item->getId()] ?? []
23772361
);
23782362
}
2379-
23802363
$this->setFlag('media_gallery_added', true);
23812364
return $this;
23822365
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,13 @@ public function loadDataFromTableByValueId(
126126
* @param int $attributeId
127127
* @return array
128128
* @since 101.0.0
129+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
130+
* @throws \Magento\Framework\Exception\LocalizedException
131+
* @throws \Zend_Db_Statement_Exception
129132
*/
130-
public function loadProductGalleryByAttributeId($product, $attributeId)
133+
public function loadProductGalleryByAttributeId($product, $attributeId = null)
131134
{
132-
$select = $this->createBaseLoadSelect(
133-
$product->getData($this->metadata->getLinkField()),
134-
$product->getStoreId(),
135-
$attributeId
136-
);
137-
138-
$result = $this->getConnection()->fetchAll($select);
139-
135+
$result = $this->getMediaRecords($product->getStoreId(), [$product->getId()], true);
140136
$this->removeDuplicates($result);
141137
return $result;
142138
}
@@ -200,9 +196,10 @@ public function getMediaRecords(int $storeId, array $entityIds, bool $preserveSo
200196
* @param int $attributeId
201197
* @return \Magento\Framework\DB\Select
202198
* @throws \Magento\Framework\Exception\LocalizedException
199+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
203200
* @since 101.0.1
204201
*/
205-
public function createBatchBaseSelect($storeId, $attributeId)
202+
public function createBatchBaseSelect($storeId, $attributeId = null)
206203
{
207204
$linkField = $this->metadata->getLinkField();
208205

@@ -266,7 +263,6 @@ public function createBatchBaseSelect($storeId, $attributeId)
266263
)->order(
267264
$positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC
268265
);
269-
270266
return $select;
271267
}
272268

0 commit comments

Comments
 (0)