Skip to content

Commit f027a46

Browse files
committed
Merge remote-tracking branch 'origin/MC-18327' into 2.2-develop-pr105
2 parents 9e1f803 + be2e842 commit f027a46

File tree

2 files changed

+115
-59
lines changed
  • app/code/Magento/Catalog/Model/Indexer/Product/Flat
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Product/Flat/Action

2 files changed

+115
-59
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Flat/TableBuilder.php

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
namespace Magento\Catalog\Model\Indexer\Product\Flat;
77

88
use Magento\Catalog\Model\Indexer\Product\Flat\Table\BuilderInterfaceFactory;
9+
use Magento\Store\Model\Store;
910

11+
/**
12+
* Flat product indexer temporary table builder.
13+
*/
1014
class TableBuilder
1115
{
1216
/**
@@ -257,75 +261,70 @@ protected function _fillTemporaryTable(
257261
$valueFieldSuffix,
258262
$storeId
259263
) {
260-
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
261264
if (!empty($tableColumns)) {
262-
$columnsChunks = array_chunk(
263-
$tableColumns,
264-
Action\Indexer::ATTRIBUTES_CHUNK_SIZE,
265-
true
266-
);
265+
$columnsChunks = array_chunk($tableColumns, Action\Indexer::ATTRIBUTES_CHUNK_SIZE / 2, true);
266+
267+
$entityTableName = $this->_productIndexerHelper->getTable('catalog_product_entity');
268+
$entityTemporaryTableName = $this->_getTemporaryTableName($entityTableName);
269+
$temporaryTableName = $this->_getTemporaryTableName($tableName);
270+
$temporaryValueTableName = $temporaryTableName . $valueFieldSuffix;
271+
$attributeOptionValueTableName = $this->_productIndexerHelper->getTable('eav_attribute_option_value');
272+
273+
$flatColumns = $this->_productIndexerHelper->getFlatColumns();
274+
$defaultStoreId = Store::DEFAULT_STORE_ID;
275+
$linkField = $this->getMetadataPool()
276+
->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
277+
->getLinkField();
278+
267279
foreach ($columnsChunks as $columnsList) {
268280
$select = $this->_connection->select();
269281
$selectValue = $this->_connection->select();
270-
$entityTableName = $this->_getTemporaryTableName(
271-
$this->_productIndexerHelper->getTable('catalog_product_entity')
272-
);
273-
$temporaryTableName = $this->_getTemporaryTableName($tableName);
274-
$temporaryValueTableName = $temporaryTableName . $valueFieldSuffix;
275-
$keyColumn = array_unique([$metadata->getLinkField(), 'entity_id']);
282+
$keyColumn = array_unique([$linkField, 'entity_id']);
276283
$columns = array_merge($keyColumn, array_keys($columnsList));
277284
$valueColumns = $keyColumn;
278-
$flatColumns = $this->_productIndexerHelper->getFlatColumns();
279285
$iterationNum = 1;
280286

281-
$select->from(['et' => $entityTableName], $keyColumn)
282-
->join(
283-
['e' => $this->resource->getTableName('catalog_product_entity')],
284-
'e.entity_id = et.entity_id',
285-
[]
286-
);
287+
$select->from(['et' => $entityTemporaryTableName], $keyColumn)
288+
->join(['e' => $entityTableName], 'e.entity_id = et.entity_id', []);
287289

288290
$selectValue->from(['e' => $temporaryTableName], $keyColumn);
289291

290292
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
291293
foreach ($columnsList as $columnName => $attribute) {
292294
$countTableName = 't' . ($iterationNum++);
293-
$joinCondition = sprintf(
294-
'e.%3$s = %1$s.%3$s' .
295-
' AND %1$s.attribute_id = %2$d' .
296-
' AND (%1$s.store_id = %4$d' .
297-
' OR %1$s.store_id = 0)',
298-
$countTableName,
299-
$attribute->getId(),
300-
$metadata->getLinkField(),
301-
$storeId
302-
);
303-
295+
$joinCondition = 'e.%3$s = %1$s.%3$s AND %1$s.attribute_id = %2$d AND %1$s.store_id = %4$d';
304296
$select->joinLeft(
305297
[$countTableName => $tableName],
306-
$joinCondition,
307-
[$columnName => 'value']
298+
sprintf($joinCondition, $countTableName, $attribute->getId(), $linkField, $defaultStoreId),
299+
[]
300+
)->joinLeft(
301+
['s' . $countTableName => $tableName],
302+
sprintf($joinCondition, 's' . $countTableName, $attribute->getId(), $linkField, $storeId),
303+
[]
308304
);
309305

306+
$columnValue = $this->_connection->getIfNullSql(
307+
's' . $countTableName . '.value',
308+
$countTableName . '.value'
309+
);
310+
$select->columns([$columnName => $columnValue]);
311+
310312
if ($attribute->getFlatUpdateSelect($storeId) instanceof \Magento\Framework\DB\Select) {
311313
$attributeCode = $attribute->getAttributeCode();
312314
$columnValueName = $attributeCode . $valueFieldSuffix;
313315
if (isset($flatColumns[$columnValueName])) {
314-
$valueJoinCondition = sprintf(
315-
'e.%1$s = %2$s.option_id AND (%2$s.store_id = %3$d OR %2$s.store_id = 0)',
316-
$attributeCode,
317-
$countTableName,
318-
$storeId
319-
);
316+
$valueJoinCondition = 'e.%1$s = %2$s.option_id AND %2$s.store_id = %3$d';
320317
$selectValue->joinLeft(
321-
[
322-
$countTableName => $this->_productIndexerHelper->getTable(
323-
'eav_attribute_option_value'
324-
),
325-
],
326-
$valueJoinCondition,
327-
[$columnValueName => $countTableName . '.value']
318+
[$countTableName => $attributeOptionValueTableName],
319+
sprintf($valueJoinCondition, $attributeCode, $countTableName, $defaultStoreId),
320+
[]
321+
)->joinLeft(
322+
['s' . $countTableName => $attributeOptionValueTableName],
323+
sprintf($valueJoinCondition, $attributeCode, 's' . $countTableName, $storeId),
324+
[]
328325
);
326+
327+
$selectValue->columns([$columnValueName => $columnValue]);
329328
$valueColumns[] = $columnValueName;
330329
}
331330
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Indexer/Product/Flat/Action/FullTest.php

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,44 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Flat\Action;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Block\Product\ListProduct;
10+
use Magento\Catalog\Model\CategoryFactory;
11+
use Magento\Catalog\Model\Indexer\Product\Flat\Processor;
12+
use Magento\Catalog\Model\Indexer\Product\Flat\State;
13+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\ObjectManager;
17+
818
/**
919
* Full reindex Test
1020
*/
1121
class FullTest extends \Magento\TestFramework\Indexer\TestCase
1222
{
1323
/**
14-
* @var \Magento\Catalog\Model\Indexer\Product\Flat\State
24+
* @var State
1525
*/
1626
protected $_state;
1727

1828
/**
19-
* @var \Magento\Catalog\Model\Indexer\Product\Flat\Processor
29+
* @var Processor
2030
*/
2131
protected $_processor;
2232

33+
/**
34+
* @var ObjectManager
35+
*/
36+
private $objectManager;
37+
38+
/**
39+
* @inheritdoc
40+
*/
2341
protected function setUp()
2442
{
25-
$this->_state = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
26-
\Magento\Catalog\Model\Indexer\Product\Flat\State::class
27-
);
28-
$this->_processor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
29-
\Magento\Catalog\Model\Indexer\Product\Flat\Processor::class
30-
);
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->_state = $this->objectManager->get(State::class);
45+
$this->_processor = $this->objectManager->get(Processor::class);
3146
}
3247

3348
/**
@@ -41,12 +56,8 @@ public function testReindexAll()
4156
$this->assertTrue($this->_state->isFlatEnabled());
4257
$this->_processor->reindexAll();
4358

44-
$categoryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
45-
\Magento\Catalog\Model\CategoryFactory::class
46-
);
47-
$listProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
48-
\Magento\Catalog\Block\Product\ListProduct::class
49-
);
59+
$categoryFactory = $this->objectManager->get(CategoryFactory::class);
60+
$listProduct = $this->objectManager->get(ListProduct::class);
5061

5162
$category = $categoryFactory->create()->load(2);
5263
$layer = $listProduct->getLayer();
@@ -61,4 +72,50 @@ public function testReindexAll()
6172
$this->assertEquals('Short description', $product->getShortDescription());
6273
}
6374
}
75+
76+
/**
77+
* @magentoAppArea frontend
78+
* @magentoDbIsolation disabled
79+
* @magentoAppIsolation enabled
80+
* @magentoDataFixture Magento/Catalog/_files/product_simple_multistore.php
81+
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_product 1
82+
* @magentoConfigFixture fixturestore_store catalog/frontend/flat_catalog_product 1
83+
*/
84+
public function testReindexAllMultipleStores()
85+
{
86+
$this->assertTrue($this->_state->isFlatEnabled());
87+
$this->_processor->reindexAll();
88+
89+
/** @var ProductCollectionFactory $productCollectionFactory */
90+
$productCollectionFactory = $this->objectManager->create(ProductCollectionFactory::class);
91+
/** @var StoreManagerInterface $storeManager */
92+
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
93+
$store = $storeManager->getStore('fixturestore');
94+
$defaultStore = $storeManager->getDefaultStoreView();
95+
96+
$expectedData = [
97+
$defaultStore->getId() => 'Simple Product One',
98+
$store->getId() => 'StoreTitle',
99+
];
100+
101+
foreach ($expectedData as $storeId => $productName) {
102+
$storeManager->setCurrentStore($storeId);
103+
$productCollection = $productCollectionFactory->create();
104+
105+
$this->assertTrue(
106+
$productCollection->isEnabledFlat(),
107+
'Flat should be enabled for product collection.'
108+
);
109+
110+
$productCollection->addIdFilter(1)->addAttributeToSelect(ProductInterface::NAME);
111+
112+
$this->assertEquals(
113+
$productName,
114+
$productCollection->getFirstItem()->getName(),
115+
'Wrong product name specified per store.'
116+
);
117+
}
118+
119+
$storeManager->setCurrentStore($defaultStore->getId());
120+
}
64121
}

0 commit comments

Comments
 (0)