From 3593bc1c4953d4f5157d7453a1d3e4c9ee3a24fb Mon Sep 17 00:00:00 2001 From: Freek Vandeursen Date: Thu, 19 Oct 2017 11:16:39 +0200 Subject: [PATCH 1/4] Improve attribute checking On a store with a large number of attribute sets, a lot of repeated checking is done for the same attributes. So instead we can keep track of the attributes we already checked, and skip them the next time. --- .../Import/Product/Type/AbstractType.php | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 5681b1aa6607d..0f694f593d5ec 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -28,6 +28,13 @@ abstract class AbstractType */ public static $commonAttributesCache = []; + /** + * Maintain a list of invisible attributes + * + * @var array + */ + public static $invisibleAttributesCache = []; + /** * Attribute Code to Id cache * @@ -278,7 +285,10 @@ protected function _initAttributes() } } foreach ($absentKeys as $attributeSetName => $attributeIds) { - $this->attachAttributesById($attributeSetName, $attributeIds); + $unknownAttributeIds = array_diff($attributeIds, array_keys(self::$commonAttributesCache), self::$invisibleAttributesCache); + if ($unknownAttributeIds) { + $this->attachAttributesById($attributeSetName, $attributeIds); + } } foreach ($entityAttributes as $attributeRow) { if (isset(self::$commonAttributesCache[$attributeRow['attribute_id']])) { @@ -310,30 +320,35 @@ protected function attachAttributesById($attributeSetName, $attributeIds) $attributeId = $attribute->getId(); if ($attribute->getIsVisible() || in_array($attributeCode, $this->_forcedAttributesCodes)) { - self::$commonAttributesCache[$attributeId] = [ - 'id' => $attributeId, - 'code' => $attributeCode, - 'is_global' => $attribute->getIsGlobal(), - 'is_required' => $attribute->getIsRequired(), - 'is_unique' => $attribute->getIsUnique(), - 'frontend_label' => $attribute->getFrontendLabel(), - 'is_static' => $attribute->isStatic(), - 'apply_to' => $attribute->getApplyTo(), - 'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute), - 'default_value' => strlen( - $attribute->getDefaultValue() - ) ? $attribute->getDefaultValue() : null, - 'options' => $this->_entityModel->getAttributeOptions( - $attribute, - $this->_indexValueAttributes - ), - ]; + if (!isset(self::$commonAttributesCache[$attributeId])) { + self::$commonAttributesCache[$attributeId] = [ + 'id' => $attributeId, + 'code' => $attributeCode, + 'is_global' => $attribute->getIsGlobal(), + 'is_required' => $attribute->getIsRequired(), + 'is_unique' => $attribute->getIsUnique(), + 'frontend_label' => $attribute->getFrontendLabel(), + 'is_static' => $attribute->isStatic(), + 'apply_to' => $attribute->getApplyTo(), + 'type' => \Magento\ImportExport\Model\Import::getAttributeType($attribute), + 'default_value' => strlen( + $attribute->getDefaultValue() + ) ? $attribute->getDefaultValue() : null, + 'options' => $this->_entityModel->getAttributeOptions( + $attribute, + $this->_indexValueAttributes + ), + ]; + } + self::$attributeCodeToId[$attributeCode] = $attributeId; $this->_addAttributeParams( $attributeSetName, self::$commonAttributesCache[$attributeId], $attribute ); + } else { + self::$invisibleAttributesCache[] = $attributeId; } } } From 61c3c48c104bb28025b76fb7a92212be7384fe00 Mon Sep 17 00:00:00 2001 From: Freek Vandeursen Date: Thu, 19 Oct 2017 13:53:00 +0200 Subject: [PATCH 2/4] Fix code style, too long line --- .../Model/Import/Product/Type/AbstractType.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 0f694f593d5ec..7b24d45bdbeb7 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -285,7 +285,11 @@ protected function _initAttributes() } } foreach ($absentKeys as $attributeSetName => $attributeIds) { - $unknownAttributeIds = array_diff($attributeIds, array_keys(self::$commonAttributesCache), self::$invisibleAttributesCache); + $unknownAttributeIds = array_diff( + $attributeIds, + array_keys(self::$commonAttributesCache), + self::$invisibleAttributesCache + ); if ($unknownAttributeIds) { $this->attachAttributesById($attributeSetName, $attributeIds); } From 976008dd78736752c0ccd87e6a24c15f61fc9ba0 Mon Sep 17 00:00:00 2001 From: Freek Vandeursen Date: Thu, 26 Oct 2017 11:00:55 +0200 Subject: [PATCH 3/4] Fix too long variable name --- .../Model/Import/Product/Type/AbstractType.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 7b24d45bdbeb7..56ecc75056b0f 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -33,7 +33,7 @@ abstract class AbstractType * * @var array */ - public static $invisibleAttributesCache = []; + public static $invAttributesCache = []; /** * Attribute Code to Id cache @@ -288,7 +288,7 @@ protected function _initAttributes() $unknownAttributeIds = array_diff( $attributeIds, array_keys(self::$commonAttributesCache), - self::$invisibleAttributesCache + self::$invAttributesCache ); if ($unknownAttributeIds) { $this->attachAttributesById($attributeSetName, $attributeIds); @@ -352,7 +352,7 @@ protected function attachAttributesById($attributeSetName, $attributeIds) $attribute ); } else { - self::$invisibleAttributesCache[] = $attributeId; + self::$invAttributesCache[] = $attributeId; } } } From a9617266df719bae3f5a2dbe707a56fc1d61fa6d Mon Sep 17 00:00:00 2001 From: Freek Vandeursen Date: Wed, 16 May 2018 00:09:15 +0200 Subject: [PATCH 4/4] Handle forced attribute codes --- .../Model/Import/Product/Type/AbstractType.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php index 94e70f6ad62da..99fe2e58c2405 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product/Type/AbstractType.php @@ -290,7 +290,7 @@ protected function _initAttributes() array_keys(self::$commonAttributesCache), self::$invAttributesCache ); - if ($unknownAttributeIds) { + if ($unknownAttributeIds || $this->_forcedAttributesCodes) { $this->attachAttributesById($attributeSetName, $attributeIds); } } @@ -317,8 +317,11 @@ protected function _initAttributes() protected function attachAttributesById($attributeSetName, $attributeIds) { foreach ($this->_prodAttrColFac->create()->addFieldToFilter( - 'main_table.attribute_id', - ['in' => $attributeIds] + ['main_table.attribute_id', 'main_table.attribute_code'], + [ + ['in' => $attributeIds], + ['in' => $this->_forcedAttributesCodes] + ] ) as $attribute) { $attributeCode = $attribute->getAttributeCode(); $attributeId = $attribute->getId();