From 1dedff04a83306fdaeab368461008e1f441a2e5d Mon Sep 17 00:00:00 2001 From: Tim Bezhashvyly Date: Sun, 6 Apr 2014 22:52:14 +0200 Subject: [PATCH] Fix ImportExport bug which occurs while importing multiple rows per entity --- .../Model/Import/AbstractEntity.php | 51 ++++++++++++------- .../Import/Entity/Eav/AbstractCustomer.php | 7 +++ .../Model/Import/Entity/Product.php | 7 +++ 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/ImportExport/Model/Import/AbstractEntity.php b/app/code/Magento/ImportExport/Model/Import/AbstractEntity.php index 774ec251dc3d7..d4ee1284446b7 100644 --- a/app/code/Magento/ImportExport/Model/Import/AbstractEntity.php +++ b/app/code/Magento/ImportExport/Model/Import/AbstractEntity.php @@ -243,6 +243,13 @@ abstract class AbstractEntity */ protected $_bunchSize; + /** + * Code of a primary attribute which identifies the entity group if import contains of multiple rows + * + * @var string + */ + protected $masterAttributeCode; + /** * Core store config * @@ -335,22 +342,23 @@ protected function _prepareRowForDb(array $rowData) protected function _saveValidatedBunches() { $source = $this->getSource(); - $processedDataSize = 0; $bunchRows = array(); $startNewBunch = false; - $nextRowBackup = array(); $source->rewind(); $this->_dataSourceModel->cleanBunches(); - while ($source->valid() || $bunchRows) { + while ($source->valid() || count($bunchRows) || isset($entityGroup)) { if ($startNewBunch || !$source->valid()) { + /* If the end approached add last validated entity group to the bunch */ + if (!$source->valid() && isset($entityGroup)) { + $bunchRows = array_merge($bunchRows, $entityGroup); + unset($entityGroup); + } $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows); - $bunchRows = $nextRowBackup; - $processedDataSize = strlen(serialize($bunchRows)); + $bunchRows = array(); $startNewBunch = false; - $nextRowBackup = array(); } if ($source->valid()) { // errors limit check @@ -358,21 +366,30 @@ protected function _saveValidatedBunches() return $this; } $rowData = $source->current(); - // add row to bunch for save - if ($this->validateRow($rowData, $source->key())) { - $rowData = $this->_prepareRowForDb($rowData); - $rowSize = strlen($this->_jsonHelper->jsonEncode($rowData)); - $isBunchSizeExceeded = $this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize; + if (isset($rowData[$this->masterAttributeCode]) && trim($rowData[$this->masterAttributeCode])) { + /* Add entity group that passed validation to bunch */ + if (isset($entityGroup)) { + $bunchRows = array_merge($bunchRows, $entityGroup); + $productDataSize = strlen(serialize($bunchRows)); - if ($processedDataSize + $rowSize >= $this->_maxDataSize || $isBunchSizeExceeded) { - $startNewBunch = true; - $nextRowBackup = array($source->key() => $rowData); - } else { - $bunchRows[$source->key()] = $rowData; - $processedDataSize += $rowSize; + /* Check if the nw bunch should be started */ + $isBunchSizeExceeded = ($this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize); + $startNewBunch = $productDataSize >= $this->_maxDataSize || $isBunchSizeExceeded; } + + /* And start a new one */ + $entityGroup = array(); } + + if (isset($entityGroup) && $this->validateRow($rowData, $source->key())) { + /* Add row to entity group */ + $entityGroup[$source->key()] = $this->_prepareRowForDb($rowData); + } elseif (isset($entityGroup)) { + /* In case validation of one line of the group fails kill the entire group */ + unset($entityGroup); + } + $this->_processedRowsCount++; $source->next(); } diff --git a/app/code/Magento/ImportExport/Model/Import/Entity/Eav/AbstractCustomer.php b/app/code/Magento/ImportExport/Model/Import/Entity/Eav/AbstractCustomer.php index c35c8566b9c9e..c23db54dc3598 100644 --- a/app/code/Magento/ImportExport/Model/Import/Entity/Eav/AbstractCustomer.php +++ b/app/code/Magento/ImportExport/Model/Import/Entity/Eav/AbstractCustomer.php @@ -80,6 +80,13 @@ abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entit */ protected $_storageFactory; + /** + * Code of a primary attribute which identifies the entity group if import contains of multiple rows + * + * @var string + */ + protected $masterAttributeCode = '_email'; + /** * @param \Magento\Core\Helper\Data $coreData * @param \Magento\Stdlib\String $string diff --git a/app/code/Magento/ImportExport/Model/Import/Entity/Product.php b/app/code/Magento/ImportExport/Model/Import/Entity/Product.php index d2b158a320356..d5f7908a20a25 100644 --- a/app/code/Magento/ImportExport/Model/Import/Entity/Product.php +++ b/app/code/Magento/ImportExport/Model/Import/Entity/Product.php @@ -455,6 +455,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity */ protected $dateTime; + /** + * Code of a primary attribute which identifies the entity group if import contains of multiple rows + * + * @var string + */ + protected $masterAttributeCode = 'sku'; + /** * @var \Magento\Logger */