|
6 | 6 | namespace Magento\Catalog\Model\Indexer\Product\Flat;
|
7 | 7 |
|
8 | 8 | use Magento\Catalog\Model\Indexer\Product\Flat\Table\BuilderInterfaceFactory;
|
| 9 | +use Magento\Store\Model\Store; |
9 | 10 |
|
10 | 11 | /**
|
11 | 12 | * Class TableBuilder
|
| 13 | + * |
| 14 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
12 | 15 | */
|
13 | 16 | class TableBuilder
|
14 | 17 | {
|
@@ -272,75 +275,70 @@ protected function _fillTemporaryTable(
|
272 | 275 | $valueFieldSuffix,
|
273 | 276 | $storeId
|
274 | 277 | ) {
|
275 |
| - $metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class); |
276 | 278 | if (!empty($tableColumns)) {
|
277 |
| - $columnsChunks = array_chunk( |
278 |
| - $tableColumns, |
279 |
| - Action\Indexer::ATTRIBUTES_CHUNK_SIZE, |
280 |
| - true |
281 |
| - ); |
| 279 | + $columnsChunks = array_chunk($tableColumns, Action\Indexer::ATTRIBUTES_CHUNK_SIZE / 2, true); |
| 280 | + |
| 281 | + $entityTableName = $this->_productIndexerHelper->getTable('catalog_product_entity'); |
| 282 | + $entityTemporaryTableName = $this->_getTemporaryTableName($entityTableName); |
| 283 | + $temporaryTableName = $this->_getTemporaryTableName($tableName); |
| 284 | + $temporaryValueTableName = $temporaryTableName . $valueFieldSuffix; |
| 285 | + $attributeOptionValueTableName = $this->_productIndexerHelper->getTable('eav_attribute_option_value'); |
| 286 | + |
| 287 | + $flatColumns = $this->_productIndexerHelper->getFlatColumns(); |
| 288 | + $defaultStoreId = Store::DEFAULT_STORE_ID; |
| 289 | + $linkField = $this->getMetadataPool() |
| 290 | + ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class) |
| 291 | + ->getLinkField(); |
| 292 | + |
282 | 293 | foreach ($columnsChunks as $columnsList) {
|
283 | 294 | $select = $this->_connection->select();
|
284 | 295 | $selectValue = $this->_connection->select();
|
285 |
| - $entityTableName = $this->_getTemporaryTableName( |
286 |
| - $this->_productIndexerHelper->getTable('catalog_product_entity') |
287 |
| - ); |
288 |
| - $temporaryTableName = $this->_getTemporaryTableName($tableName); |
289 |
| - $temporaryValueTableName = $temporaryTableName . $valueFieldSuffix; |
290 |
| - $keyColumn = array_unique([$metadata->getLinkField(), 'entity_id']); |
| 296 | + $keyColumn = array_unique([$linkField, 'entity_id']); |
291 | 297 | $columns = array_merge($keyColumn, array_keys($columnsList));
|
292 | 298 | $valueColumns = $keyColumn;
|
293 |
| - $flatColumns = $this->_productIndexerHelper->getFlatColumns(); |
294 | 299 | $iterationNum = 1;
|
295 | 300 |
|
296 |
| - $select->from(['et' => $entityTableName], $keyColumn) |
297 |
| - ->join( |
298 |
| - ['e' => $this->resource->getTableName('catalog_product_entity')], |
299 |
| - 'e.entity_id = et.entity_id', |
300 |
| - [] |
301 |
| - ); |
| 301 | + $select->from(['et' => $entityTemporaryTableName], $keyColumn) |
| 302 | + ->join(['e' => $entityTableName], 'e.entity_id = et.entity_id', []); |
302 | 303 |
|
303 | 304 | $selectValue->from(['e' => $temporaryTableName], $keyColumn);
|
304 | 305 |
|
305 | 306 | /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
|
306 | 307 | foreach ($columnsList as $columnName => $attribute) {
|
307 | 308 | $countTableName = 't' . ($iterationNum++);
|
308 |
| - $joinCondition = sprintf( |
309 |
| - 'e.%3$s = %1$s.%3$s' . |
310 |
| - ' AND %1$s.attribute_id = %2$d' . |
311 |
| - ' AND (%1$s.store_id = %4$d' . |
312 |
| - ' OR %1$s.store_id = 0)', |
313 |
| - $countTableName, |
314 |
| - $attribute->getId(), |
315 |
| - $metadata->getLinkField(), |
316 |
| - $storeId |
317 |
| - ); |
318 |
| - |
| 309 | + $joinCondition = 'e.%3$s = %1$s.%3$s AND %1$s.attribute_id = %2$d AND %1$s.store_id = %4$d'; |
319 | 310 | $select->joinLeft(
|
320 | 311 | [$countTableName => $tableName],
|
321 |
| - $joinCondition, |
322 |
| - [$columnName => 'value'] |
| 312 | + sprintf($joinCondition, $countTableName, $attribute->getId(), $linkField, $defaultStoreId), |
| 313 | + [] |
| 314 | + )->joinLeft( |
| 315 | + ['s' . $countTableName => $tableName], |
| 316 | + sprintf($joinCondition, 's' . $countTableName, $attribute->getId(), $linkField, $storeId), |
| 317 | + [] |
323 | 318 | );
|
324 | 319 |
|
| 320 | + $columnValue = $this->_connection->getIfNullSql( |
| 321 | + 's' . $countTableName . '.value', |
| 322 | + $countTableName . '.value' |
| 323 | + ); |
| 324 | + $select->columns([$columnName => $columnValue]); |
| 325 | + |
325 | 326 | if ($attribute->getFlatUpdateSelect($storeId) instanceof \Magento\Framework\DB\Select) {
|
326 | 327 | $attributeCode = $attribute->getAttributeCode();
|
327 | 328 | $columnValueName = $attributeCode . $valueFieldSuffix;
|
328 | 329 | if (isset($flatColumns[$columnValueName])) {
|
329 |
| - $valueJoinCondition = sprintf( |
330 |
| - 'e.%1$s = %2$s.option_id AND (%2$s.store_id = %3$d OR %2$s.store_id = 0)', |
331 |
| - $attributeCode, |
332 |
| - $countTableName, |
333 |
| - $storeId |
334 |
| - ); |
| 330 | + $valueJoinCondition = 'e.%1$s = %2$s.option_id AND %2$s.store_id = %3$d'; |
335 | 331 | $selectValue->joinLeft(
|
336 |
| - [ |
337 |
| - $countTableName => $this->_productIndexerHelper->getTable( |
338 |
| - 'eav_attribute_option_value' |
339 |
| - ), |
340 |
| - ], |
341 |
| - $valueJoinCondition, |
342 |
| - [$columnValueName => $countTableName . '.value'] |
| 332 | + [$countTableName => $attributeOptionValueTableName], |
| 333 | + sprintf($valueJoinCondition, $attributeCode, $countTableName, $defaultStoreId), |
| 334 | + [] |
| 335 | + )->joinLeft( |
| 336 | + ['s' . $countTableName => $attributeOptionValueTableName], |
| 337 | + sprintf($valueJoinCondition, $attributeCode, 's' . $countTableName, $storeId), |
| 338 | + [] |
343 | 339 | );
|
| 340 | + |
| 341 | + $selectValue->columns([$columnValueName => $columnValue]); |
344 | 342 | $valueColumns[] = $columnValueName;
|
345 | 343 | }
|
346 | 344 | }
|
|
0 commit comments