Skip to content

Commit a9797cd

Browse files
author
Ganin, Roman(rganin)
committed
Merge pull request #351 from magento-troll/MAGETWO-43939
[Troll] Category staging
2 parents 730e198 + 2446bbd commit a9797cd

File tree

60 files changed

+769
-181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+769
-181
lines changed

app/code/Magento/Catalog/Model/Category.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* @method array getAffectedCategoryIds()
2626
* @method Category setUrlKey(string $urlKey)
2727
* @method Category setUrlPath(string $urlPath)
28+
* @method Category getSkipDeleteChildren()
29+
* @method Category setSkipDeleteChildren(boolean $value)
2830
*
2931
* @SuppressWarnings(PHPMD.LongVariable)
3032
* @SuppressWarnings(PHPMD.ExcessivePublicCount)

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\Catalog\Model\Indexer\Category\Flat;
1010

1111
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\Model\Entity\MetadataPool;
1213

1314
class AbstractAction
1415
{
@@ -53,6 +54,18 @@ class AbstractAction
5354
*/
5455
protected $connection;
5556

57+
/**
58+
* @var \Magento\Framework\Model\Entity\EntityMetadata
59+
*/
60+
protected $categoryMetadata;
61+
62+
/**
63+
* Static columns to skip
64+
*
65+
* @var array
66+
*/
67+
protected $skipStaticColumns = [];
68+
5669
/**
5770
* @param ResourceConnection $resource
5871
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -61,13 +74,17 @@ class AbstractAction
6174
public function __construct(
6275
ResourceConnection $resource,
6376
\Magento\Store\Model\StoreManagerInterface $storeManager,
64-
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
77+
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
78+
MetadataPool $metadataPool,
79+
$skipStaticColumns = []
6580
) {
6681
$this->resource = $resource;
6782
$this->connection = $resource->getConnection();
6883
$this->storeManager = $storeManager;
6984
$this->resourceHelper = $resourceHelper;
85+
$this->skipStaticColumns = $skipStaticColumns;
7086
$this->columns = array_merge($this->getStaticColumns(), $this->getEavColumns());
87+
$this->categoryMetadata = $metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class);
7188
}
7289

7390
/**
@@ -177,13 +194,12 @@ protected function getFlatTableStructure($tableName)
177194
protected function getStaticColumns()
178195
{
179196
$columns = [];
180-
$columnsToSkip = ['entity_type_id', 'attribute_set_id'];
181197
$describe = $this->connection->describeTable(
182198
$this->connection->getTableName($this->getTableName('catalog_category_entity'))
183199
);
184200

185201
foreach ($describe as $column) {
186-
if (in_array($column['COLUMN_NAME'], $columnsToSkip)) {
202+
if (in_array($column['COLUMN_NAME'], $this->skipStaticColumns)) {
187203
continue;
188204
}
189205
$isUnsigned = '';
@@ -364,11 +380,11 @@ protected function getAttributeValues($entityIds, $storeId)
364380
$attributesType = ['varchar', 'int', 'decimal', 'text', 'datetime'];
365381
foreach ($attributesType as $type) {
366382
foreach ($this->getAttributeTypeValues($type, $entityIds, $storeId) as $row) {
367-
if (isset($row['entity_id']) && isset($row['attribute_id'])) {
383+
if (isset($row[$this->categoryMetadata->getLinkField()]) && isset($row['attribute_id'])) {
368384
$attributeId = $row['attribute_id'];
369385
if (isset($attributes[$attributeId])) {
370386
$attributeCode = $attributes[$attributeId]['attribute_code'];
371-
$values[$row['entity_id']][$attributeCode] = $row['value'];
387+
$values[$row[$this->categoryMetadata->getLinkField()]][$attributeCode] = $row['value'];
372388
}
373389
}
374390
}
@@ -386,18 +402,24 @@ protected function getAttributeValues($entityIds, $storeId)
386402
*/
387403
protected function getAttributeTypeValues($type, $entityIds, $storeId)
388404
{
405+
$linkField = $this->categoryMetadata->getLinkField();
389406
$select = $this->connection->select()->from(
390407
[
391408
'def' => $this->connection->getTableName($this->getTableName('catalog_category_entity_' . $type)),
392409
],
393-
['entity_id', 'attribute_id']
410+
[$linkField, 'attribute_id']
411+
)->joinLeft(
412+
[
413+
'e' => $this->connection->getTableName($this->getTableName('catalog_category_entity'))
414+
],
415+
"def.{$linkField} = e.{$linkField}"
394416
)->joinLeft(
395417
[
396418
'store' => $this->connection->getTableName(
397419
$this->getTableName('catalog_category_entity_' . $type)
398420
),
399421
],
400-
'store.entity_id = def.entity_id AND store.attribute_id = def.attribute_id ' .
422+
"store.{$linkField} = def.{$linkField} AND store.attribute_id = def.attribute_id " .
401423
'AND store.store_id = ' .
402424
$storeId,
403425
[
@@ -408,7 +430,7 @@ protected function getAttributeTypeValues($type, $entityIds, $storeId)
408430
)
409431
]
410432
)->where(
411-
'def.entity_id IN (?)',
433+
"e.entity_id IN (?)",
412434
$entityIds
413435
)->where(
414436
'def.store_id IN (?)',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ protected function populateFlatTables(array $stores)
6868
$attributesData = $this->getAttributeValues($categoriesIdsChunk, $store->getId());
6969
$data = [];
7070
foreach ($categories[$store->getRootCategoryId()] as $category) {
71-
if (!isset($attributesData[$category['entity_id']])) {
71+
if (!isset($attributesData[$category[$this->categoryMetadata->getLinkField()]])) {
7272
continue;
7373
}
7474
$category['store_id'] = $store->getId();
7575
$data[] = $this->prepareValuesToInsert(
76-
array_merge($category, $attributesData[$category['entity_id']])
76+
array_merge($category, $attributesData[$category[$this->categoryMetadata->getLinkField()]])
7777
);
7878
}
7979
$this->connection->insertMultiple(

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
99
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Model\Entity\MetadataPool;
1011

1112
class Rows extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction
1213
{
@@ -19,16 +20,20 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Flat\AbstractAction
1920
* @param \Magento\Framework\App\ResourceConnection $resource
2021
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
2122
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
23+
* @param MetadataPool $metadataPool
24+
* @param array $skipStaticColumns
2225
* @param CategoryRepositoryInterface $categoryRepository
2326
*/
2427
public function __construct(
2528
\Magento\Framework\App\ResourceConnection $resource,
2629
\Magento\Store\Model\StoreManagerInterface $storeManager,
2730
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
28-
CategoryRepositoryInterface $categoryRepository
31+
MetadataPool $metadataPool,
32+
CategoryRepositoryInterface $categoryRepository,
33+
$skipStaticColumns = []
2934
) {
3035
$this->categoryRepository = $categoryRepository;
31-
parent::__construct($resource, $storeManager, $resourceHelper);
36+
parent::__construct($resource, $storeManager, $resourceHelper, $metadataPool, $skipStaticColumns);
3237
}
3338

3439
/**
@@ -122,14 +127,16 @@ protected function deleteNonStoreCategories(\Magento\Store\Model\Store $store, $
122127
/** @var \Magento\Framework\DB\Select $select */
123128
$select = $this->connection->select()->from(
124129
['cf' => $this->getTableNameByStore($store, $useTempTable)]
125-
)->joinLeft(
126-
['ce' => $this->getTableName('catalog_category_entity')],
127-
'cf.path = ce.path',
128-
[]
129130
)->where(
130131
"cf.path = {$rootIdExpr} OR cf.path = {$rootCatIdExpr} OR cf.path like {$catIdExpr}"
131132
)->where(
132-
'ce.entity_id IS NULL'
133+
'cf.entity_id NOT IN (?)',
134+
new \Zend_Db_Expr(
135+
$this->connection->select()->from(
136+
['ce' => $this->getTableName('catalog_category_entity')],
137+
['entity_id']
138+
)
139+
)
133140
);
134141

135142
$sql = $select->deleteFromSelect('cf');
@@ -157,7 +164,7 @@ protected function filterIdsByStore(array $ids, $store)
157164
)->where(
158165
"path = {$rootIdExpr} OR path = {$rootCatIdExpr} OR path like {$catIdExpr}"
159166
)->where(
160-
'entity_id IN (?)',
167+
"entity_id IN (?)",
161168
$ids
162169
);
163170

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,11 @@ protected function createAnchorSelect(\Magento\Store\Model\Store $store)
371371
$rootCatIds = explode('/', $this->getPathFromCategoryId($store->getRootCategoryId()));
372372
array_pop($rootCatIds);
373373

374-
$metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
375-
$linkField = $metadata->getLinkField();
374+
$productMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
375+
$categoryMetadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\CategoryInterface::class);
376+
$productLinkField = $productMetadata->getLinkField();
377+
$categoryLinkField = $categoryMetadata->getLinkField();
378+
376379
return $this->connection->select()->from(
377380
['cc' => $this->getTable('catalog_category_entity')],
378381
[]
@@ -399,35 +402,36 @@ protected function createAnchorSelect(\Magento\Store\Model\Store $store)
399402
[]
400403
)->joinInner(
401404
['cpsd' => $this->getTable('catalog_product_entity_int')],
402-
'cpsd.' . $linkField . ' = cpe.' . $linkField . ' AND cpsd.store_id = 0'
405+
'cpsd.' . $productLinkField . ' = cpe.' . $productLinkField . ' AND cpsd.store_id = 0'
403406
. ' AND cpsd.attribute_id = ' . $statusAttributeId,
404407
[]
405408
)->joinLeft(
406409
['cpss' => $this->getTable('catalog_product_entity_int')],
407-
'cpss.' . $linkField . ' = cpe.' . $linkField . ' AND cpss.attribute_id = cpsd.attribute_id' .
410+
'cpss.' . $productLinkField . ' = cpe.' . $productLinkField . ' AND cpss.attribute_id = cpsd.attribute_id' .
408411
' AND cpss.store_id = ' .
409412
$store->getId(),
410413
[]
411414
)->joinInner(
412415
['cpvd' => $this->getTable('catalog_product_entity_int')],
413-
'cpvd.' . $linkField . ' = cpe. ' . $linkField . ' AND cpvd.store_id = 0' .
416+
'cpvd.' . $productLinkField . ' = cpe. ' . $productLinkField . ' AND cpvd.store_id = 0' .
414417
' AND cpvd.attribute_id = ' .
415418
$visibilityAttributeId,
416419
[]
417420
)->joinLeft(
418421
['cpvs' => $this->getTable('catalog_product_entity_int')],
419-
'cpvs.' . $linkField . ' = cpe.' . $linkField . ' AND cpvs.attribute_id = cpvd.attribute_id ' .
420-
'AND cpvs.store_id = ' .
422+
'cpvs.' . $productLinkField . ' = cpe.' . $productLinkField .
423+
' AND cpvs.attribute_id = cpvd.attribute_id ' . 'AND cpvs.store_id = ' .
421424
$store->getId(),
422425
[]
423426
)->joinInner(
424427
['ccad' => $this->getTable('catalog_category_entity_int')],
425-
'ccad.entity_id = cc.entity_id AND ccad.store_id = 0' . ' AND ccad.attribute_id = ' . $isAnchorAttributeId,
428+
'ccad.' . $categoryLinkField . ' = cc.' . $categoryLinkField . ' AND ccad.store_id = 0' .
429+
' AND ccad.attribute_id = ' . $isAnchorAttributeId,
426430
[]
427431
)->joinLeft(
428432
['ccas' => $this->getTable('catalog_category_entity_int')],
429-
'ccas.entity_id = cc.entity_id AND ccas.attribute_id = ccad.attribute_id' .
430-
' AND ccas.store_id = ' .
433+
'ccas.' . $categoryLinkField . ' = cc.' . $categoryLinkField
434+
. ' AND ccas.attribute_id = ccad.attribute_id AND ccas.store_id = ' .
431435
$store->getId(),
432436
[]
433437
)->where(

app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected function _getLoadAttributesSelect($object, $table)
140140
$select = $this->getConnection()
141141
->select()
142142
->from(['attr_table' => $table], [])
143-
->where("attr_table.{$this->getLinkField()} = ?", $object->getId())
143+
->where("attr_table.{$this->getLinkField()} = ?", $object->getData($this->getLinkField()))
144144
->where('attr_table.store_id IN (?)', $storeIds);
145145

146146
if ($setId) {
@@ -227,13 +227,14 @@ protected function _saveAttributeValue($object, $attribute, $value)
227227
* for default store id
228228
* In this case we clear all not default values
229229
*/
230+
$entityIdField = $this->getLinkField();
230231
if ($this->_storeManager->hasSingleStore()) {
231232
$storeId = $this->getDefaultStoreId();
232233
$connection->delete(
233234
$table,
234235
[
235236
'attribute_id = ?' => $attribute->getAttributeId(),
236-
$this->getLinkField() . ' = ?' => $object->getId(),
237+
"{$entityIdField} = ?" => $object->getData($entityIdField),
237238
'store_id <> ?' => $storeId
238239
]
239240
);
@@ -243,7 +244,7 @@ protected function _saveAttributeValue($object, $attribute, $value)
243244
[
244245
'attribute_id' => $attribute->getAttributeId(),
245246
'store_id' => $storeId,
246-
$this->getLinkField() => $object->getData($this->getLinkField()),
247+
$entityIdField => $object->getData($entityIdField),
247248
'value' => $this->_prepareValueForSave($value, $attribute),
248249
]
249250
);
@@ -296,15 +297,15 @@ protected function _insertAttribute($object, $attribute, $value)
296297
->from($table)
297298
->where('attribute_id = ?', $attribute->getAttributeId())
298299
->where('store_id = ?', $this->getDefaultStoreId())
299-
->where('entity_id = ?', $object->getId());
300+
->where($this->getLinkField() . ' = ?', $object->getData($this->getLinkField()));
300301
$row = $this->getConnection()->fetchOne($select);
301302

302303
if (!$row) {
303304
$data = new \Magento\Framework\DataObject(
304305
[
305306
'attribute_id' => $attribute->getAttributeId(),
306307
'store_id' => $this->getDefaultStoreId(),
307-
'entity_id' => $object->getId(),
308+
$this->getLinkField() => $object->getData($this->getLinkField()),
308309
'value' => $this->_prepareValueForSave($value, $attribute),
309310
]
310311
);
@@ -563,7 +564,7 @@ public function getAttributeRawValue($entityId, $attribute, $store)
563564
['e' => $this->getTable('catalog_product_entity')],
564565
'e.' . $this->getLinkField() . ' = ' . $staticTable . '.' . $this->getLinkField()
565566
)->where(
566-
'e.entity_id = :entity_id'
567+
'e.entity_id = :entity_id'
567568
);
568569
$attributesData = $connection->fetchRow($select, ['entity_id' => $entityId]);
569570
}

0 commit comments

Comments
 (0)