Skip to content

Commit 9179379

Browse files
author
Volodymyr Kublytskyi
authored
MAGETWO-84529: [2.3] Correct flat indexing with staging and > 500 cats #12345
2 parents 84c1beb + 43fb519 commit 9179379

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Flat/AbstractAction.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,22 +369,55 @@ protected function getAttributeValues($entityIds, $storeId)
369369
}
370370
$values = [];
371371

372+
$linkIds = $this->getLinkIds($entityIds);
373+
foreach ($linkIds as $linkId) {
374+
$values[$linkId] = [];
375+
}
376+
372377
$attributes = $this->getAttributes();
373378
$attributesType = ['varchar', 'int', 'decimal', 'text', 'datetime'];
379+
$linkField = $this->getCategoryMetadata()->getLinkField();
374380
foreach ($attributesType as $type) {
375381
foreach ($this->getAttributeTypeValues($type, $entityIds, $storeId) as $row) {
376-
if (isset($row['entity_id']) && isset($row['attribute_id'])) {
382+
if (isset($row[$linkField]) && isset($row['attribute_id'])) {
377383
$attributeId = $row['attribute_id'];
378384
if (isset($attributes[$attributeId])) {
379385
$attributeCode = $attributes[$attributeId]['attribute_code'];
380-
$values[$row['entity_id']][$attributeCode] = $row['value'];
386+
$values[$row[$linkField]][$attributeCode] = $row['value'];
381387
}
382388
}
383389
}
384390
}
391+
385392
return $values;
386393
}
387394

395+
/**
396+
* Translate entity ids into link ids
397+
*
398+
* Used for rows with no EAV attributes set.
399+
*
400+
* @param array $entityIds
401+
* @return array
402+
*/
403+
private function getLinkIds(array $entityIds)
404+
{
405+
$linkField = $this->getCategoryMetadata()->getLinkField();
406+
if ($linkField === 'entity_id') {
407+
return $entityIds;
408+
}
409+
410+
$select = $this->connection->select()->from(
411+
['e' => $this->connection->getTableName($this->getTableName('catalog_category_entity'))],
412+
[$linkField]
413+
)->where(
414+
'e.entity_id IN (?)',
415+
$entityIds
416+
);
417+
418+
return $this->connection->fetchCol($select);
419+
}
420+
388421
/**
389422
* Return attribute values for given entities and store of specific attribute type
390423
*

app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Full.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,22 @@ protected function populateFlatTables(array $stores)
6464
}
6565
/** @TODO Do something with chunks */
6666
$categoriesIdsChunks = array_chunk($categoriesIds[$store->getRootCategoryId()], 500);
67+
6768
foreach ($categoriesIdsChunks as $categoriesIdsChunk) {
6869
$attributesData = $this->getAttributeValues($categoriesIdsChunk, $store->getId());
70+
$linkField = $this->categoryMetadata->getLinkField();
71+
6972
$data = [];
7073
foreach ($categories[$store->getRootCategoryId()] as $category) {
71-
if (!isset($attributesData[$category[$this->categoryMetadata->getLinkField()]])) {
74+
if (!isset($attributesData[$category[$linkField]])) {
7275
continue;
7376
}
7477
$category['store_id'] = $store->getId();
7578
$data[] = $this->prepareValuesToInsert(
76-
array_merge($category, $attributesData[$category[$this->categoryMetadata->getLinkField()]])
79+
array_merge($category, $attributesData[$category[$linkField]])
7780
);
7881
}
82+
7983
$this->connection->insertMultiple(
8084
$this->addTemporaryTableSuffix($this->getMainStoreTable($store->getId())),
8185
$data

app/code/Magento/Catalog/Model/Indexer/Category/Flat/Action/Rows.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,22 @@ private function reindexStore(Store $store, array $entityIds, $useTempTable)
164164
*/
165165
private function buildIndexData(Store $store, $categoriesIdsChunk, $attributesData)
166166
{
167+
$linkField = $this->categoryMetadata->getLinkField();
168+
167169
$data = [];
168170
foreach ($categoriesIdsChunk as $categoryId) {
169171
try {
172+
$category = $this->categoryRepository->get($categoryId);
173+
$categoryData = $category->getData();
174+
$linkId = $categoryData[$linkField];
175+
170176
$categoryAttributesData = [];
171-
if (isset($attributesData[$categoryId]) && is_array($attributesData[$categoryId])) {
172-
$categoryAttributesData = $attributesData[$categoryId];
177+
if (isset($attributesData[$linkId]) && is_array($attributesData[$linkId])) {
178+
$categoryAttributesData = $attributesData[$linkId];
173179
}
174180
$categoryIndexData = $this->buildCategoryIndexData(
175181
$store,
176-
$categoryId,
182+
$categoryData,
177183
$categoryAttributesData
178184
);
179185
$data[] = $categoryIndexData;
@@ -186,18 +192,16 @@ private function buildIndexData(Store $store, $categoriesIdsChunk, $attributesDa
186192

187193
/**
188194
* @param Store $store
189-
* @param int $categoryId
195+
* @param array $categoryData
190196
* @param array $categoryAttributesData
191197
* @return array
192198
* @throws NoSuchEntityException
193199
*/
194-
private function buildCategoryIndexData(Store $store, $categoryId, array $categoryAttributesData)
200+
private function buildCategoryIndexData(Store $store, array $categoryData, array $categoryAttributesData)
195201
{
196-
$category = $this->categoryRepository->get($categoryId);
197-
$categoryAttributesData = [];
198202
$data = $this->prepareValuesToInsert(
199203
array_merge(
200-
$category->getData(),
204+
$categoryData,
201205
$categoryAttributesData,
202206
['store_id' => $store->getId()]
203207
)

0 commit comments

Comments
 (0)