Skip to content

Commit

Permalink
magento#18624 Refactoring: Extract Database code
Browse files Browse the repository at this point in the history
  • Loading branch information
amenk committed Mar 9, 2019
1 parent 5be2075 commit 28cd9d1
Showing 1 changed file with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,8 @@ public function saveLinks($entityModel, $productEntityLinkField)
$mainTable = $this->_resource->getMainTable();
$positionAttrId = [];
$nextLinkId = $this->_resourceHelper->getNextAutoincrement($mainTable);
$positionAttrId = $this->loadPositionAttributes($positionAttrId);

// pre-load 'position' attributes ID for each link type once
foreach ($this->_linkNameToId as $linkName => $linkId) {
$select = $this->_connection->select()->from(
$this->_resource->getTable('catalog_product_link_attribute'),
['id' => 'product_link_attribute_id']
)->where(
'link_type_id = :link_id AND product_link_attribute_code = :position'
);
$bind = [':link_id' => $linkId, ':position' => 'position'];
$positionAttrId[$linkId] = $this->_connection->fetchOne($select, $bind);
}
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$productIds = [];
$linkRows = [];
Expand Down Expand Up @@ -174,23 +164,9 @@ public function saveLinks($entityModel, $productEntityLinkField)
}
}
}
if (Import::BEHAVIOR_APPEND !== $this->_entityModel->getBehavior() && $productIds) {
$this->_connection->delete(
$mainTable,
$this->_connection->quoteInto('product_id IN (?)', array_unique($productIds))
);
}
if ($linkRows) {
$this->_connection->insertOnDuplicate($mainTable, $linkRows, ['link_id']);
}
if ($positionRows) {
// process linked product positions
$this->_connection->insertOnDuplicate(
$this->_resource->getAttributeTypeTable('int'),
$positionRows,
['value']
);
}

$this->deleteExistingLinks($productIds);
$this->insertNewLinks($linkRows, $positionRows);
}

return $this;
Expand Down Expand Up @@ -294,4 +270,59 @@ protected function getLinkSkus($rowData, string $linkName)

return explode($this->_entityModel->getMultipleValueSeparator(), $rowData[$linkField]);
}

/**
* pre-load 'position' attributes ID for each link type once
*
* @param array $positionAttrId
*
* @return array
*/
protected function loadPositionAttributes(array $positionAttrId): array
{
foreach ($this->_linkNameToId as $linkName => $linkId) {
$select = $this->_connection->select()->from(
$this->_resource->getTable('catalog_product_link_attribute'),
['id' => 'product_link_attribute_id']
)->where(
'link_type_id = :link_id AND product_link_attribute_code = :position'
);
$bind = [':link_id' => $linkId, ':position' => 'position'];
$positionAttrId[$linkId] = $this->_connection->fetchOne($select, $bind);
}

return $positionAttrId;
}

/**
* @param array $productIds
*/
protected function deleteExistingLinks(array $productIds): void
{
if (Import::BEHAVIOR_APPEND !== $this->_entityModel->getBehavior() && $productIds) {
$this->_connection->delete(
$this->_resource->getMainTable(),
$this->_connection->quoteInto('product_id IN (?)', array_unique($productIds))
);
}
}

/**
* @param array $linkRows
* @param array $positionRows
*/
protected function insertNewLinks(array $linkRows, array $positionRows): void
{
if ($linkRows) {
$this->_connection->insertOnDuplicate($this->_resource->getMainTable(), $linkRows, ['link_id']);
}
if ($positionRows) {
// process linked product positions
$this->_connection->insertOnDuplicate(
$this->_resource->getAttributeTypeTable('int'),
$positionRows,
['value']
);
}
}
}

0 comments on commit 28cd9d1

Please sign in to comment.