Skip to content

Commit 48c2f62

Browse files
Senthilkumar MuppidathiSenthilkumar Muppidathi
authored andcommitted
#40217 - Introduced _setItemAttributeValues function and deprecate _setItemAttributeValue
1 parent 189dd72 commit 48c2f62

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ public function _loadAttributes($printQuery = false, $logQuery = false)
12531253
}
12541254

12551255
if ($data) {
1256-
$this->_setItemAttributeValue($data);
1256+
$this->_setItemAttributeValues($data);
12571257
}
12581258
}
12591259
}
@@ -1322,26 +1322,64 @@ protected function _addLoadAttributesSelectValues($select, $table, $type)
13221322
/**
13231323
* Initialize entity object property value
13241324
*
1325-
* Parameter $valueInfo is [product_id => [attribute_code => value]]
1325+
* Parameter $valueInfo is _getLoadAttributesSelect fetch result row
1326+
*
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.
13261329
*
13271330
* @param array $valueInfo
13281331
* @return $this
13291332
* @throws LocalizedException
13301333
*/
13311334
protected function _setItemAttributeValue($valueInfo)
13321335
{
1333-
foreach ($valueInfo as $entityId => $value) {
1336+
$entityIdField = $this->getEntity()->getEntityIdField();
1337+
$entityId = $valueInfo[$entityIdField];
1338+
if (!isset($this->_itemsById[$entityId])) {
1339+
throw new LocalizedException(
1340+
__('A header row is missing for an attribute. Verify the header row and try again.')
1341+
);
1342+
}
1343+
$attributeCode = array_search($valueInfo['attribute_id'], $this->_selectAttributes);
1344+
if (!$attributeCode) {
1345+
$attribute = $this->_eavConfig->getAttribute(
1346+
$this->getEntity()->getType(),
1347+
$valueInfo['attribute_id']
1348+
);
1349+
$attributeCode = $attribute->getAttributeCode();
1350+
}
1351+
1352+
foreach ($this->_itemsById[$entityId] as $object) {
1353+
$object->setData($attributeCode, $valueInfo['value']);
1354+
}
1355+
1356+
return $this;
1357+
}
1358+
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) {
13341371
if (!isset($this->_itemsById[$entityId])) {
13351372
throw new LocalizedException(
13361373
__('A header row is missing for an attribute. Verify the header row and try again.')
13371374
);
13381375
}
1339-
// Run only once
1376+
// _itemsById[$entityId] is always an array (typically with one element)
1377+
// foreach handles edge cases where multiple objects share the same entity ID
13401378
foreach ($this->_itemsById[$entityId] as $object) {
1341-
$object->setData($object->getData()+$value);
1379+
$object->setData($object->getData()+$attributeValues);
13421380
}
1343-
}
13441381

1382+
}
13451383
return $this;
13461384
}
13471385

0 commit comments

Comments
 (0)