Skip to content

Commit

Permalink
Merge pull request #1162 from magento-tsg/2.1.8-develop-pr16
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr16) (2.1.8)
  • Loading branch information
Volodymyr Klymenko committed Jun 6, 2017
2 parents 88ed064 + 58d13c8 commit 400f4ee
Show file tree
Hide file tree
Showing 79 changed files with 2,843 additions and 754 deletions.
Expand Up @@ -542,19 +542,24 @@ protected function retrieveOldSkus()
*/
protected function processCountExistingPrices($prices, $table)
{
$oldSkus = $this->retrieveOldSkus();
$existProductIds = array_intersect_key($oldSkus, $prices);
if (!count($existProductIds)) {
return $this;
}

$tableName = $this->_resourceFactory->create()->getTable($table);
$productEntityLinkField = $this->getProductEntityLinkField();
$existingPrices = $this->_connection->fetchAssoc(
$this->_connection->select()->from(
$tableName,
['value_id', $productEntityLinkField, 'all_groups', 'customer_group_id']
)
)->where($productEntityLinkField . ' IN (?)', $existProductIds)
);
$oldSkus = $this->retrieveOldSkus();
foreach ($existingPrices as $existingPrice) {
foreach ($oldSkus as $sku => $productId) {
if ($existingPrice[$productEntityLinkField] == $productId && isset($prices[$sku])) {
$this->incrementCounterUpdated($prices[$sku], $existingPrice);
foreach ($prices as $sku => $skuPrices) {
if (isset($oldSkus[$sku]) && $existingPrice[$productEntityLinkField] == $oldSkus[$sku]) {
$this->incrementCounterUpdated($skuPrices, $existingPrice);
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions app/code/Magento/Catalog/Model/Product/Copier.php
Expand Up @@ -9,6 +9,9 @@

use Magento\Catalog\Api\Data\ProductInterface;

/**
* Catalog product copier.
*/
class Copier
{
/**
Expand Down Expand Up @@ -54,12 +57,15 @@ public function copy(\Magento\Catalog\Model\Product $product)
$product->getWebsiteIds();
$product->getCategoryIds();

/** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);

/** @var \Magento\Catalog\Model\Product $duplicate */
$duplicate = $this->productFactory->create();
$duplicate->setData($product->getData());
$duplicate->setOptions([]);
$duplicate->setIsDuplicate(true);
$duplicate->setOriginalId($product->getEntityId());
$duplicate->setOriginalLinkId($product->getData($metadata->getLinkField()));
$duplicate->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$duplicate->setCreatedAt(null);
$duplicate->setUpdatedAt(null);
Expand All @@ -81,11 +87,11 @@ public function copy(\Magento\Catalog\Model\Product $product)
}
} while (!$isDuplicateSaved);
$this->getOptionRepository()->duplicate($product, $duplicate);
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
$product->getResource()->duplicate(
$product->getData($metadata->getLinkField()),
$duplicate->getData($metadata->getLinkField())
);

return $duplicate;
}

Expand All @@ -97,8 +103,9 @@ private function getOptionRepository()
{
if (null === $this->optionRepository) {
$this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Catalog\Model\Product\Option\Repository');
->get(\Magento\Catalog\Model\Product\Option\Repository::class);
}

return $this->optionRepository;
}

Expand All @@ -110,8 +117,9 @@ private function getMetadataPool()
{
if (null === $this->metadataPool) {
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\EntityManager\MetadataPool');
->get(\Magento\Framework\EntityManager\MetadataPool::class);
}

return $this->metadataPool;
}
}
Expand Up @@ -278,6 +278,8 @@ protected function processNewImage($product, array &$image)
}

/**
* Duplicate product media gallery data.
*
* @param \Magento\Catalog\Model\Product $product
* @return $this
*/
Expand All @@ -294,7 +296,7 @@ protected function duplicate($product)
$this->resourceModel->duplicate(
$this->getAttribute()->getAttributeId(),
isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [],
$product->getOriginalId(),
$product->getOriginalLinkId(),
$product->getData($this->metadata->getLinkField())
);

Expand Down
Expand Up @@ -2213,16 +2213,24 @@ public function addMediaGalleryData()
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
$items = $this->getItems();

$select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) {
return $item->getId();
}, $items));
$select->where(
'entity.' . $linkField . ' IN (?)',
array_map(
function ($item) use ($linkField) {
return $item->getData($linkField);
},
$items
)
);

foreach ($this->getConnection()->fetchAll($select) as $row) {
$mediaGalleries[$row[$linkField]][] = $row;
}

foreach ($items as $item) {
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
$mediaEntries = isset($mediaGalleries[$item->getData($linkField)])
? $mediaGalleries[$item->getData($linkField)]
: [];
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
}

Expand Down
Expand Up @@ -197,17 +197,25 @@ protected function _prepareRelationIndexSelect($parentIds = null)
)->joinLeft(
['e' => $this->getTable('catalog_product_entity')],
'e.' . $linkField .' = l.parent_id',
['e.entity_id as parent_id']
[]
)->join(
['cs' => $this->getTable('store')],
'',
[]
)->join(
['i' => $idxTable],
'l.child_id = i.entity_id AND cs.store_id = i.store_id',
['attribute_id', 'store_id', 'value']
[]
)->group(
['parent_id', 'i.attribute_id', 'i.store_id', 'i.value']
['parent_id', 'i.attribute_id', 'i.store_id', 'i.value', 'l.child_id']
)->columns(
[
'parent_id' => 'e.entity_id',
'attribute_id' => 'i.attribute_id',
'store_id' => 'i.store_id',
'value' => 'i.value',
'source_id' => 'l.child_id'
]
);
if ($parentIds !== null) {
$select->where('e.entity_id IN(?)', $parentIds);
Expand All @@ -222,7 +230,7 @@ protected function _prepareRelationIndexSelect($parentIds = null)
'select' => $select,
'entity_field' => new \Zend_Db_Expr('l.parent_id'),
'website_field' => new \Zend_Db_Expr('cs.website_id'),
'store_field' => new \Zend_Db_Expr('cs.store_id')
'store_field' => new \Zend_Db_Expr('cs.store_id'),
]
);

Expand Down
Expand Up @@ -85,6 +85,7 @@ protected function _prepareIndex($entityIds = null, $attributeId = null)
'pdd.attribute_id',
'cs.store_id',
'value' => $productValueExpression,
'source_id' => 'cpe.entity_id',
]
);

Expand Down Expand Up @@ -116,7 +117,7 @@ protected function _prepareIndex($entityIds = null, $attributeId = null)
'select' => $select,
'entity_field' => new \Zend_Db_Expr('cpe.entity_id'),
'website_field' => new \Zend_Db_Expr('cs.website_id'),
'store_field' => new \Zend_Db_Expr('cs.store_id')
'store_field' => new \Zend_Db_Expr('cs.store_id'),
]
);

Expand Down
Expand Up @@ -178,6 +178,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
'pid.attribute_id',
'pid.store_id',
'value' => $ifNullSql,
'pid.entity_id',
]
)->where(
'pid.attribute_id IN(?)',
Expand All @@ -200,7 +201,7 @@ protected function _prepareSelectIndex($entityIds = null, $attributeId = null)
'select' => $select,
'entity_field' => new \Zend_Db_Expr('pid.entity_id'),
'website_field' => new \Zend_Db_Expr('pid.website_id'),
'store_field' => new \Zend_Db_Expr('pid.store_id')
'store_field' => new \Zend_Db_Expr('pid.store_id'),
]
);
$query = $select->insertFromSelect($idxTable);
Expand All @@ -221,11 +222,7 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
$connection = $this->getConnection();

// prepare multiselect attributes
if ($attributeId === null) {
$attrIds = $this->_getIndexableAttributes(true);
} else {
$attrIds = [$attributeId];
}
$attrIds = $attributeId === null ? $this->_getIndexableAttributes(true) : [$attributeId];

if (!$attrIds) {
return $this;
Expand All @@ -247,20 +244,20 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
$productValueExpression = $connection->getCheckSql('pvs.value_id > 0', 'pvs.value', 'pvd.value');
$select = $connection->select()->from(
['pvd' => $this->getTable('catalog_product_entity_varchar')],
[$productIdField, 'attribute_id']
[]
)->join(
['cs' => $this->getTable('store')],
'',
['store_id']
[]
)->joinLeft(
['pvs' => $this->getTable('catalog_product_entity_varchar')],
"pvs.{$productIdField} = pvd.{$productIdField} AND pvs.attribute_id = pvd.attribute_id"
. ' AND pvs.store_id=cs.store_id',
['value' => $productValueExpression]
[]
)->joinLeft(
['cpe' => $this->getTable('catalog_product_entity')],
"cpe.{$productIdField} = pvd.{$productIdField}",
['entity_id']
['']
)->where(
'pvd.store_id=?',
$connection->getIfNullSql('pvs.store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
Expand All @@ -272,6 +269,14 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
$attrIds
)->where(
'cpe.entity_id IS NOT NULL'
)->columns(
[
'entity_id' => 'cpe.entity_id',
'attribute_id' => 'attribute_id',
'store_id' => 'cs.store_id',
'value' => $productValueExpression,
'source_id' => 'cpe.entity_id',
]
);

$statusCond = $connection->quoteInto('=?', ProductStatus::STATUS_ENABLED);
Expand All @@ -289,30 +294,11 @@ protected function _prepareMultiselectIndex($entityIds = null, $attributeId = nu
'select' => $select,
'entity_field' => new \Zend_Db_Expr('cpe.entity_id'),
'website_field' => new \Zend_Db_Expr('cs.website_id'),
'store_field' => new \Zend_Db_Expr('cs.store_id')
'store_field' => new \Zend_Db_Expr('cs.store_id'),
]
);

$i = 0;
$data = [];
$query = $select->query();
while ($row = $query->fetch()) {
$values = explode(',', $row['value']);
foreach ($values as $valueId) {
if (isset($options[$row['attribute_id']][$valueId])) {
$data[] = [$row['entity_id'], $row['attribute_id'], $row['store_id'], $valueId];
$i++;
if ($i % 10000 == 0) {
$this->_saveIndexData($data);
$data = [];
}
}
}
}

$this->_saveIndexData($data);
unset($options);
unset($data);
$this->saveDataFromSelect($select, $options);

return $this;
}
Expand All @@ -331,12 +317,43 @@ protected function _saveIndexData(array $data)
$connection = $this->getConnection();
$connection->insertArray(
$this->getIdxTable(),
['entity_id', 'attribute_id', 'store_id', 'value'],
['entity_id', 'attribute_id', 'store_id', 'value', 'source_id'],
$data
);

return $this;
}

/**
* Prepares data from select to save.
*
* @param \Magento\Framework\DB\Select $select
* @param array $options
*
* @return void
*/
private function saveDataFromSelect(\Magento\Framework\DB\Select $select, array $options)
{
$i = 0;
$data = [];
$query = $select->query();
while ($row = $query->fetch()) {
$values = explode(',', $row['value']);
foreach ($values as $valueId) {
if (isset($options[$row['attribute_id']][$valueId])) {
$data[] = [$row['entity_id'], $row['attribute_id'], $row['store_id'], $valueId, $row['source_id']];
$i++;
if ($i % 10000 == 0) {
$this->_saveIndexData($data);
$data = [];
}
}
}
}

$this->_saveIndexData($data);
}

/**
* Retrieve temporary source index table name
*
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Setup/InstallSchema.php
Expand Up @@ -18,6 +18,7 @@ class InstallSchema implements InstallSchemaInterface
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @throws \Zend_Db_Exception
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
Expand Down Expand Up @@ -2426,7 +2427,6 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
'option_id',
$installer->getTable('catalog_product_option'),
'option_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE,
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)
->setComment(
Expand Down

0 comments on commit 400f4ee

Please sign in to comment.