diff --git a/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php b/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php index d8762eb604f00..737a9f07c61f0 100644 --- a/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php +++ b/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php @@ -1,7 +1,7 @@ getEntityIdField(); + foreach ($values as $value) { - $this->_setItemAttributeValue($value); + $entityId = $value[$entityIdField]; + $attributeId = $value['attribute_id']; + if (!isset($attributeCode[$attributeId])) { + $attributeCode[$attributeId] = array_search($attributeId, $this->_selectAttributes); + if (!$attributeCode[$attributeId]) { + $attribute = $this->_eavConfig->getAttribute( + $this->getEntity()->getType(), + $attributeId + ); + $attributeCode[$attributeId] = $attribute->getAttributeCode(); + } + } + $data[$entityId][$attributeCode[$attributeId]] = $value['value']; + } + + if ($data) { + $this->_setItemAttributeValues($data); } } } @@ -1305,6 +1324,9 @@ protected function _addLoadAttributesSelectValues($select, $table, $type) * * Parameter $valueInfo is _getLoadAttributesSelect fetch result row * + * @deprecated Batch process of attribute values is introduced to reduce time complexity. + * @see _setItemAttributeValues($entityAttributeMap) uses array union (+) to acheive O(n) complexity. + * * @param array $valueInfo * @return $this * @throws LocalizedException @@ -1334,6 +1356,33 @@ protected function _setItemAttributeValue($valueInfo) return $this; } + /** + * Initialize entity object property value + * + * Parameter $entityAttributeMap is [entity_id => [attribute_code => value, ...]] + * + * @param array $entityAttributeMap + * @return $this + * @throws LocalizedException + */ + protected function _setItemAttributeValues(array $entityAttributeMap) + { + foreach ($entityAttributeMap as $entityId => $attributeValues) { + if (!isset($this->_itemsById[$entityId])) { + throw new LocalizedException( + __('A header row is missing for an attribute. Verify the header row and try again.') + ); + } + // _itemsById[$entityId] is always an array (typically with one element) + // foreach handles edge cases where multiple objects share the same entity ID + foreach ($this->_itemsById[$entityId] as $object) { + $object->setData($object->getData()+$attributeValues); + } + + } + return $this; + } + /** * Get alias for attribute value table *