Skip to content

Commit 0dc6ce5

Browse files
Merge remote-tracking branch '40217/Fixing-BigOComplexity-in-Eav' into commpr-21755-0511
2 parents 1492845 + 7a4f0b7 commit 0dc6ce5

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2013 Adobe
3+
* Copyright 2025 Adobe
44
* All Rights Reserved.
55
*/
66

@@ -1233,8 +1233,27 @@ public function _loadAttributes($printQuery = false, $logQuery = false)
12331233
throw $e;
12341234
}
12351235

1236+
$attributeCode = $data = [];
1237+
$entityIdField = $entity->getEntityIdField();
1238+
12361239
foreach ($values as $value) {
1237-
$this->_setItemAttributeValue($value);
1240+
$entityId = $value[$entityIdField];
1241+
$attributeId = $value['attribute_id'];
1242+
if (!isset($attributeCode[$attributeId])) {
1243+
$attributeCode[$attributeId] = array_search($attributeId, $this->_selectAttributes);
1244+
if (!$attributeCode[$attributeId]) {
1245+
$attribute = $this->_eavConfig->getAttribute(
1246+
$this->getEntity()->getType(),
1247+
$attributeId
1248+
);
1249+
$attributeCode[$attributeId] = $attribute->getAttributeCode();
1250+
}
1251+
}
1252+
$data[$entityId][$attributeCode[$attributeId]] = $value['value'];
1253+
}
1254+
1255+
if ($data) {
1256+
$this->_setItemAttributeValues($data);
12381257
}
12391258
}
12401259
}
@@ -1305,6 +1324,9 @@ protected function _addLoadAttributesSelectValues($select, $table, $type)
13051324
*
13061325
* Parameter $valueInfo is _getLoadAttributesSelect fetch result row
13071326
*
1327+
* @deprecated Batch process of attribute values is introduced to reduce time complexity.
1328+
* @see _setItemAttributeValues($entityAttributeMap) uses array union (+) to acheive O(n) complexity.
1329+
*
13081330
* @param array $valueInfo
13091331
* @return $this
13101332
* @throws LocalizedException
@@ -1334,6 +1356,33 @@ protected function _setItemAttributeValue($valueInfo)
13341356
return $this;
13351357
}
13361358

1359+
/**
1360+
* Initialize entity object property value
1361+
*
1362+
* Parameter $entityAttributeMap is [entity_id => [attribute_code => value, ...]]
1363+
*
1364+
* @param array $entityAttributeMap
1365+
* @return $this
1366+
* @throws LocalizedException
1367+
*/
1368+
protected function _setItemAttributeValues(array $entityAttributeMap)
1369+
{
1370+
foreach ($entityAttributeMap as $entityId => $attributeValues) {
1371+
if (!isset($this->_itemsById[$entityId])) {
1372+
throw new LocalizedException(
1373+
__('A header row is missing for an attribute. Verify the header row and try again.')
1374+
);
1375+
}
1376+
// _itemsById[$entityId] is always an array (typically with one element)
1377+
// foreach handles edge cases where multiple objects share the same entity ID
1378+
foreach ($this->_itemsById[$entityId] as $object) {
1379+
$object->setData($object->getData()+$attributeValues);
1380+
}
1381+
1382+
}
1383+
return $this;
1384+
}
1385+
13371386
/**
13381387
* Get alias for attribute value table
13391388
*

0 commit comments

Comments
 (0)