Skip to content

Commit 541e139

Browse files
authored
Merge pull request #1088 from magento-performance/MAGETWO-62691
[Performance] EAV attributes caching optimization
2 parents f14f886 + e746bb0 commit 541e139

File tree

28 files changed

+311
-298
lines changed

28 files changed

+311
-298
lines changed

app/code/Magento/Catalog/Model/Product/Type/AbstractType.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,7 @@ public function save($product)
723723
protected function _removeNotApplicableAttributes($product)
724724
{
725725
$entityType = $product->getResource()->getEntityType();
726-
foreach ($this->_eavConfig->getEntityAttributeCodes($entityType, $product) as $attributeCode) {
727-
$attribute = $this->_eavConfig->getAttribute($entityType, $attributeCode);
726+
foreach ($this->_eavConfig->getEntityAttributes($entityType, $product) as $attribute) {
728727
$applyTo = $attribute->getApplyTo();
729728
if (is_array($applyTo) && count($applyTo) > 0 && !in_array($product->getTypeId(), $applyTo)) {
730729
$product->unsetData($attribute->getAttributeCode());

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
682682
$collection = $this->collectionFactory->create();
683683
$this->extensionAttributesJoinProcessor->process($collection);
684684

685-
foreach ($this->metadataService->getList($this->searchCriteriaBuilder->create())->getItems() as $metadata) {
686-
$collection->addAttributeToSelect($metadata->getAttributeCode());
687-
}
685+
$collection->addAttributeToSelect('*');
688686
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
689687
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
690688

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,16 @@ public function countVisible()
10111011
public function load($object, $entityId, $attributes = [])
10121012
{
10131013
$this->_attributes = [];
1014-
$this->loadAttributesMetadata($attributes);
1014+
$select = $this->_getLoadRowSelect($object, $entityId);
1015+
$row = $this->getConnection()->fetchRow($select);
1016+
1017+
if (is_array($row)) {
1018+
$object->addData($row);
1019+
} else {
1020+
$object->isObjectNew(true);
1021+
}
1022+
1023+
$this->loadAttributesForObject($attributes, $object);
10151024
$object = $this->getEntityManager()->load($object, $entityId);
10161025
if (!$this->getEntityManager()->has($object)) {
10171026
$object->isObjectNew(true);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,16 @@ public function validate($object)
577577
*/
578578
public function load($object, $entityId, $attributes = [])
579579
{
580-
$this->loadAttributesMetadata($attributes);
580+
$select = $this->_getLoadRowSelect($object, $entityId);
581+
$row = $this->getConnection()->fetchRow($select);
582+
583+
if (is_array($row)) {
584+
$object->addData($row);
585+
} else {
586+
$object->isObjectNew(true);
587+
}
588+
589+
$this->loadAttributesForObject($attributes, $object);
581590
$this->getEntityManager()->load($object, $entityId);
582591
return $this;
583592
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Gallery/ProcessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testValidate($value)
165165
$attributeCode = 'attr_code';
166166
$attribute = $this->getMock(
167167
\Magento\Eav\Model\Entity\Attribute::class,
168-
['getAttributeCode', 'getIsRequired', 'isValueEmpty', 'getIsUnique', 'getEntityType', '__wakeup'],
168+
['getAttributeCode', 'getIsRequired', 'isValueEmpty', 'getIsUnique', 'getEntity', '__wakeup'],
169169
[],
170170
'',
171171
false
@@ -178,7 +178,7 @@ public function testValidate($value)
178178
$attribute->expects($this->any())->method('getIsRequired')->will($this->returnValue(true));
179179
$attribute->expects($this->any())->method('isValueEmpty')->will($this->returnValue($value));
180180
$attribute->expects($this->any())->method('getIsUnique')->will($this->returnValue(true));
181-
$attribute->expects($this->any())->method('getEntityType')->will($this->returnValue($attributeEntity));
181+
$attribute->expects($this->any())->method('getEntity')->will($this->returnValue($attributeEntity));
182182
$attributeEntity->expects($this->any())->method('checkAttributeUniqueValue')->will($this->returnValue(true));
183183

184184
$this->attributeRepository->expects($this->once())

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -707,36 +707,17 @@ public function testDeleteById()
707707
public function testGetList()
708708
{
709709
$searchCriteriaMock = $this->getMock(\Magento\Framework\Api\SearchCriteriaInterface::class, [], [], '', false);
710-
$attributeCode = 'attribute_code';
711710
$collectionMock = $this->getMock(
712711
\Magento\Catalog\Model\ResourceModel\Product\Collection::class,
713712
[],
714713
[],
715714
'',
716715
false
717716
);
718-
$extendedSearchCriteriaMock = $this->getMock(\Magento\Framework\Api\SearchCriteria::class, [], [], '', false);
719-
$productAttributeSearchResultsMock = $this->getMockBuilder(ProductAttributeInterface::class)
720-
->disableOriginalConstructor()
721-
->setMethods(['getItems'])
722-
->getMockForAbstractClass();
723-
$productAttributeMock = $this->getMock(
724-
\Magento\Catalog\Api\Data\ProductAttributeInterface::class,
725-
[],
726-
[],
727-
'',
728-
false
729-
);
730717

731718
$this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($collectionMock);
732-
$this->searchCriteriaBuilderMock->expects($this->once())->method('create')
733-
->willReturn($extendedSearchCriteriaMock);
734-
$this->metadataServiceMock->expects($this->once())->method('getList')->with($extendedSearchCriteriaMock)
735-
->willReturn($productAttributeSearchResultsMock);
736-
$productAttributeSearchResultsMock->expects($this->once())->method('getItems')
737-
->willReturn([$productAttributeMock]);
738-
$productAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode);
739-
$collectionMock->expects($this->once())->method('addAttributeToSelect')->with($attributeCode);
719+
720+
$collectionMock->expects($this->once())->method('addAttributeToSelect')->with('*');
740721
$collectionMock->expects($this->exactly(2))->method('joinAttribute')->withConsecutive(
741722
['status', 'catalog_product/status', 'entity_id', null, 'inner'],
742723
['visibility', 'catalog_product/visibility', 'entity_id', null, 'inner']

app/code/Magento/Customer/Model/Attribute/Data/Postcode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function __construct(
4949
public function validateValue($value)
5050
{
5151
$attribute = $this->getAttribute();
52-
$label = __($attribute->getStoreLabel());
5352

5453
$countryId = $this->getExtractedData('country_id');
5554
if ($this->directoryHelper->isZipCodeOptional($countryId)) {
@@ -58,6 +57,7 @@ public function validateValue($value)
5857

5958
$errors = [];
6059
if (empty($value) && $value !== '0') {
60+
$label = __($attribute->getStoreLabel());
6161
$errors[] = __('"%1" is a required value.', $label);
6262
}
6363
if (count($errors) == 0) {

app/code/Magento/Customer/Model/AttributeMetadataConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function createMetadataAttribute($attribute)
8787
}
8888
}
8989
$validationRules = [];
90-
foreach ($attribute->getValidateRules() as $name => $value) {
90+
foreach ((array)$attribute->getValidateRules() as $name => $value) {
9191
$validationRule = $this->validationRuleFactory->create()
9292
->setName($name)
9393
->setValue($value);

app/code/Magento/Customer/Test/Unit/Model/Attribute/Data/PostcodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function setUp()
6464
public function testValidateValue($value, $expected, $countryId, $isOptional)
6565
{
6666
$storeLabel = 'Zip/Postal Code';
67-
$this->attributeMock->expects($this->once())
67+
$this->attributeMock->expects($this->any())
6868
->method('getStoreLabel')
6969
->willReturn($storeLabel);
7070

app/code/Magento/Downloadable/Test/Unit/Model/Product/TypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ function ($value) {
153153
$this->product->expects($this->any())->method('setLinksExist')->with($this->equalTo(false));
154154
$this->product->expects($this->any())->method('canAffectOptions')->with($this->equalTo(true));
155155

156-
$eavConfigMock = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityAttributeCodes'], [], '', false);
156+
$eavConfigMock = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityAttributes'], [], '', false);
157157
$eavConfigMock->expects($this->any())
158-
->method('getEntityAttributeCodes')
158+
->method('getEntityAttributes')
159159
->with($this->equalTo($entityTypeMock), $this->equalTo($this->product))
160160
->will($this->returnValue([]));
161161

0 commit comments

Comments
 (0)