Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ImportExport bug which occurs while importing multiple rows per entity #542

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions app/code/Magento/ImportExport/Model/Import/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -335,44 +342,54 @@ 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
if ($this->_errorsCount >= $this->_errorsLimit) {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/ImportExport/Model/Import/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down