Skip to content

Commit b747757

Browse files
committed
MAGETWO-55299: [Customer] Fast Save of Product Variations
- MAGETWO-55787: Fast saving of product with high number of variations generated
1 parent 8b30062 commit b747757

File tree

5 files changed

+126
-69
lines changed

5 files changed

+126
-69
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
238238
if (!$object->getChildrenCount()) {
239239
$object->setChildrenCount(0);
240240
}
241-
241+
$object->setAttributeSetId(
242+
$object->getAttributeSetId() ?: $this->getEntityType()->getDefaultAttributeSetId()
243+
);
242244
if ($object->isObjectNew()) {
243245
if ($object->getPosition() === null) {
244246
$object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Model\ResourceModel;
7+
8+
use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
9+
use Magento\Eav\Model\Entity\AttributeCache;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\EntityManager\MetadataPool;
12+
13+
/**
14+
* Class load and cache attributes from some attribute set
15+
*/
16+
class AttributeLoader
17+
{
18+
/** Name of ATTRIBUTE_SET_ID field */
19+
const ATTRIBUTE_SET_ID = 'attribute_set_id';
20+
21+
/**
22+
* @var AttributeRepository
23+
*/
24+
private $attributeRepository;
25+
26+
/**
27+
* @var MetadataPool
28+
*/
29+
private $metadataPool;
30+
31+
/**
32+
* @var SearchCriteriaBuilder
33+
*/
34+
private $searchCriteriaBuilder;
35+
36+
/**
37+
* @var AttributeCache
38+
*/
39+
private $attributeCache;
40+
41+
/**
42+
* AttributeLoader constructor.
43+
* @param AttributeRepository $attributeRepository
44+
* @param MetadataPool $metadataPool
45+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
46+
* @param AttributeCache $attributeCache
47+
*/
48+
public function __construct(
49+
AttributeRepository $attributeRepository,
50+
MetadataPool $metadataPool,
51+
SearchCriteriaBuilder $searchCriteriaBuilder,
52+
AttributeCache $attributeCache
53+
) {
54+
$this->attributeRepository = $attributeRepository;
55+
$this->metadataPool = $metadataPool;
56+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
57+
$this->attributeCache = $attributeCache;
58+
}
59+
60+
/**
61+
* Get attributes list from attribute set
62+
*
63+
* @param string $entityType
64+
* @param int $attributeSetId
65+
* @return \Magento\Eav\Api\Data\AttributeInterface[]|\object[]
66+
*/
67+
public function getAttributes($entityType, $attributeSetId = null)
68+
{
69+
$suffix = self::ATTRIBUTE_SET_ID . '-' . ($attributeSetId ?: 'all');
70+
if ($attributes = $this->attributeCache->getAttributes($entityType, $suffix)) {
71+
return $attributes;
72+
}
73+
74+
$metadata = $this->metadataPool->getMetadata($entityType);
75+
76+
if ($attributeSetId === null) {
77+
$criteria = $this->searchCriteriaBuilder->addFilter(self::ATTRIBUTE_SET_ID, null, 'neq')->create();
78+
} else {
79+
$criteria = $this->searchCriteriaBuilder->addFilter(self::ATTRIBUTE_SET_ID, $attributeSetId)->create();
80+
}
81+
82+
$searchResult = $this->attributeRepository->getList(
83+
$metadata->getEavEntityType(),
84+
$criteria
85+
);
86+
$attributes = $searchResult->getItems();
87+
88+
$this->attributeCache->saveAttributes(
89+
$entityType,
90+
$attributes,
91+
$suffix
92+
);
93+
return $attributes;
94+
}
95+
}

app/code/Magento/Eav/Model/ResourceModel/CreateHandler.php

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Eav\Model\ResourceModel;
77

88
use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
9-
use Magento\Eav\Model\Entity\AttributeCache;
109
use Magento\Framework\Api\SearchCriteriaBuilder;
1110
use Magento\Framework\App\ObjectManager;
1211
use Magento\Framework\EntityManager\MetadataPool;
@@ -19,9 +18,6 @@
1918
*/
2019
class CreateHandler implements AttributeInterface
2120
{
22-
/** Name of ATTRIBUTE_SET_ID field */
23-
const ATTRIBUTE_SET_ID = 'attribute_set_id';
24-
2521
/**
2622
* @var AttributeRepository
2723
*/
@@ -48,78 +44,42 @@ class CreateHandler implements AttributeInterface
4844
private $scopeResolver;
4945

5046
/**
51-
* @var AttributeCache
47+
* @var AttributeLoader
5248
*/
53-
private $attributeCache;
49+
private $attributeLoader;
5450

5551
/**
5652
* @param AttributeRepository $attributeRepository
5753
* @param MetadataPool $metadataPool
5854
* @param SearchCriteriaBuilder $searchCriteriaBuilder
5955
* @param AttributePersistor $attributePersistor
6056
* @param ScopeResolver $scopeResolver
57+
* @param AttributeLoader $attributeLoader
6158
*/
6259
public function __construct(
6360
AttributeRepository $attributeRepository,
6461
MetadataPool $metadataPool,
6562
SearchCriteriaBuilder $searchCriteriaBuilder,
6663
AttributePersistor $attributePersistor,
67-
ScopeResolver $scopeResolver
64+
ScopeResolver $scopeResolver,
65+
AttributeLoader $attributeLoader = null
6866
) {
6967
$this->attributeRepository = $attributeRepository;
7068
$this->metadataPool = $metadataPool;
7169
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
7270
$this->attributePersistor = $attributePersistor;
7371
$this->scopeResolver = $scopeResolver;
74-
}
75-
76-
/**
77-
* @deprecated
78-
* @return AttributeCache
79-
*/
80-
private function getAttributeCache()
81-
{
82-
if ($this->attributeCache === null) {
83-
$this->attributeCache = ObjectManager::getInstance()->get(AttributeCache::class);
84-
}
85-
86-
return $this->attributeCache;
72+
$this->attributeLoader = $attributeLoader ?: ObjectManager::getInstance()->get(AttributeLoader::class);
8773
}
8874

8975
/**
9076
* @param string $entityType
77+
* @param int $attributeSetId
9178
* @return \Magento\Eav\Api\Data\AttributeInterface[]
92-
* @throws \Exception
9379
*/
9480
protected function getAttributes($entityType, $attributeSetId = null)
9581
{
96-
/** @var AttributeCache $cache */
97-
$cache = $this->getAttributeCache();
98-
$suffix = 'attribute_set_id-' . ($attributeSetId ?: 'all');
99-
if ($attributes = $cache->getAttributes($entityType, $suffix)) {
100-
return $attributes;
101-
}
102-
103-
$metadata = $this->metadataPool->getMetadata($entityType);
104-
105-
if ($attributeSetId === null) {
106-
$criteria = $this->searchCriteriaBuilder->addFilter(self::ATTRIBUTE_SET_ID . '', null, 'neq')->create();
107-
} else {
108-
$criteria = $this->searchCriteriaBuilder->addFilter(self::ATTRIBUTE_SET_ID, $attributeSetId)->create();
109-
}
110-
111-
$searchResult = $this->attributeRepository->getList(
112-
$metadata->getEavEntityType(),
113-
$criteria
114-
);
115-
$attributes = $searchResult->getItems();
116-
117-
$this->attributeCache->saveAttributes(
118-
$entityType,
119-
$attributes,
120-
$suffix
121-
);
122-
return $attributes;
82+
return $this->attributeLoader->getAttributes($entityType, $attributeSetId);
12383
}
12484

12585
/**
@@ -137,9 +97,12 @@ public function execute($entityType, $entityData, $arguments = [])
13797
if ($metadata->getEavEntityType()) {
13898
$processed = [];
13999
$entityLinkField = $metadata->getLinkField();
140-
$attributeSetId = isset($entityData[self::ATTRIBUTE_SET_ID]) ? $entityData[self::ATTRIBUTE_SET_ID] : null;
100+
$attributeSetId = isset($entityData[AttributeLoader::ATTRIBUTE_SET_ID])
101+
? $entityData[AttributeLoader::ATTRIBUTE_SET_ID]
102+
: null; // @todo verify is it normal to not have attributer_set_id
141103
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
142104
foreach ($this->getAttributes($entityType, $attributeSetId) as $attribute) {
105+
$ac[] = $attribute->getAttributeCode();
143106
if ($attribute->isStatic()) {
144107
continue;
145108
}

app/code/Magento/Eav/Model/ResourceModel/UpdateHandler.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class UpdateHandler implements AttributeInterface
5454
*/
5555
private $readHandler;
5656

57+
/**
58+
* @var AttributeLoader
59+
*/
60+
private $attributeLoader;
61+
5762
/**
5863
* UpdateHandler constructor.
5964
* @param AttributeRepository $attributeRepository
@@ -69,14 +74,16 @@ public function __construct(
6974
SearchCriteriaBuilder $searchCriteriaBuilder,
7075
AttributePersistor $attributePersistor,
7176
ReadSnapshot $readSnapshot,
72-
ScopeResolver $scopeResolver
77+
ScopeResolver $scopeResolver,
78+
AttributeLoader $attributeLoader = null
7379
) {
7480
$this->attributeRepository = $attributeRepository;
7581
$this->metadataPool = $metadataPool;
7682
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
7783
$this->attributePersistor = $attributePersistor;
7884
$this->readSnapshot = $readSnapshot;
7985
$this->scopeResolver = $scopeResolver;
86+
$this->attributeLoader = $attributeLoader ?: ObjectManager::getInstance()->get(AttributeLoader::class);
8087
}
8188

8289
/**
@@ -90,26 +97,13 @@ private function getAttributeCache()
9097

9198
/**
9299
* @param string $entityType
100+
* @param int $attributeSetId
93101
* @return \Magento\Eav\Api\Data\AttributeInterface[]
94-
* @throws \Exception
95102
*/
96-
protected function getAttributes($entityType)
103+
protected function getAttributes($entityType, $attributeSetId = null)
97104
{
98-
/** @var \Magento\Eav\Model\Entity\AttributeCache $cache */
99-
$cache = $this->getAttributeCache();
100-
if ($attributes = $cache->getAttributes($entityType)) {
101-
return $attributes;
102-
}
103-
104-
$metadata = $this->metadataPool->getMetadata($entityType);
105-
106-
$searchResult = $this->attributeRepository->getList(
107-
$metadata->getEavEntityType(),
108-
$this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create()
109-
);
110-
return $searchResult->getItems();
105+
return $this->attributeLoader->getAttributes($entityType, $attributeSetId);
111106
}
112-
113107
/**
114108
* @param string $entityType
115109
* @param array $entityData
@@ -133,8 +127,11 @@ public function execute($entityType, $entityData, $arguments = [])
133127
$entityDataForSnapshot[$scope->getIdentifier()] = $entityData[$scope->getIdentifier()];
134128
}
135129
}
130+
$attributeSetId = isset($entityData[AttributeLoader::ATTRIBUTE_SET_ID])
131+
? $entityData[AttributeLoader::ATTRIBUTE_SET_ID]
132+
: null; // @todo verify is it normal to not have attributer_set_id
136133
$snapshot = $this->readSnapshot->execute($entityType, $entityDataForSnapshot);
137-
foreach ($this->getAttributes($entityType) as $attribute) {
134+
foreach ($this->getAttributes($entityType, $attributeSetId) as $attribute) {
138135
if ($attribute->isStatic()) {
139136
continue;
140137
}

dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_shipping_method_and_items_categories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
)->setId(
5757
444
5858
)->setAttributeSetId(
59-
5
59+
4
6060
)->setStoreId(
6161
1
6262
)->setWebsiteIds(

0 commit comments

Comments
 (0)