Skip to content

Commit 29ccc86

Browse files
ACPT-1544
Now checks if attribute type can has website-specific options (checks if model is_a \Magento\Eav\Model\Attribute). If not, doesn't use website-specific key in $attributes. This is to reduce the amount of attributes loaded in memory when working with multiple websites.
1 parent 3a4555e commit 29ccc86

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

app/code/Magento/Eav/Model/Config.php

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Config implements ResetAfterRequestInterface
7373
* Initialized attributes
7474
*
7575
* [int $website][string $entityTypeCode][string $code] = AbstractAttribute $attribute
76-
* @var array<int, array<string, array<string, AbstractAttribute>>>
76+
* @var array<int|null, array<string, array<string, AbstractAttribute>>>
7777
*/
7878
private $attributes;
7979

@@ -162,6 +162,9 @@ class Config implements ResetAfterRequestInterface
162162
*/
163163
private $attributesForPreload;
164164

165+
/** @var bool[] */
166+
private array $isAttributeTypeWebsiteSpecificCache = [];
167+
165168
/**
166169
* @param \Magento\Framework\App\CacheInterface $cache
167170
* @param Entity\TypeFactory $entityTypeFactory
@@ -249,7 +252,12 @@ protected function _load($id)
249252
*/
250253
private function loadAttributes($entityTypeCode)
251254
{
252-
return $this->attributes[$this->getWebsiteId()][$entityTypeCode] ?? [];
255+
if ($this->isAttributeTypeWebsiteSpecific($entityTypeCode)) {
256+
$websiteId = $this->getWebsiteId();
257+
} else {
258+
$websiteId = null;
259+
}
260+
return $this->attributes[$websiteId][$entityTypeCode] ?? [];
253261
}
254262

255263
/**
@@ -275,7 +283,12 @@ protected function _save($obj, $id)
275283
*/
276284
private function saveAttribute(AbstractAttribute $attribute, $entityTypeCode, $attributeCode)
277285
{
278-
$this->attributes[$this->getWebsiteId()][$entityTypeCode][$attributeCode] = $attribute;
286+
if ($this->isAttributeTypeWebsiteSpecific($entityTypeCode)) {
287+
$websiteId = $this->getWebsiteId();
288+
} else {
289+
$websiteId = null;
290+
}
291+
$this->attributes[$websiteId][$entityTypeCode][$attributeCode] = $attribute;
279292
}
280293

281294
/**
@@ -543,7 +556,11 @@ public function getAttributes($entityType)
543556
*/
544557
public function getAttribute($entityType, $code)
545558
{
546-
$websiteId = $this->getWebsiteId();
559+
if ($this->isAttributeTypeWebsiteSpecific($entityType)) {
560+
$websiteId = $this->getWebsiteId();
561+
} else {
562+
$websiteId = null;
563+
}
547564
if ($code instanceof \Magento\Eav\Model\Entity\Attribute\AttributeInterface) {
548565
return $code;
549566
}
@@ -912,7 +929,7 @@ public function importAttributesData($entityType, array $attributes)
912929
/**
913930
* Create attribute by attribute code
914931
*
915-
* @param string $entityType
932+
* @param string|Type $entityType
916933
* @param string $attributeCode
917934
* @return AbstractAttribute
918935
* @throws LocalizedException
@@ -1002,17 +1019,45 @@ public function getWebsiteId() : int
10021019
return (int)$websiteId;
10031020
}
10041021

1022+
/**
1023+
* Returns true if $entityType has website-specific options.
1024+
*
1025+
* Most attributes are global, but some can have website-specific options.
1026+
*
1027+
* @param string|Type $entityType
1028+
* @return bool
1029+
*/
1030+
private function isAttributeTypeWebsiteSpecific(string|Type $entityType) : bool
1031+
{
1032+
if ($entityType instanceof Type) {
1033+
$entityTypeCode = $entityType->getEntityTypeCode();
1034+
} else {
1035+
$entityTypeCode = $entityType;
1036+
}
1037+
if (key_exists($entityTypeCode, $this->isAttributeTypeWebsiteSpecificCache)) {
1038+
return $this->isAttributeTypeWebsiteSpecificCache[$entityTypeCode];
1039+
}
1040+
$entityType = $this->getEntityType($entityType);
1041+
$model = $entityType->getAttributeModel();
1042+
$returnValue = is_a($model, \Magento\Eav\Model\Attribute::class, true);
1043+
$this->isAttributeTypeWebsiteSpecificCache[$entityTypeCode] = $returnValue;
1044+
return $returnValue;
1045+
}
1046+
10051047
/**
10061048
* @inheritDoc
10071049
*/
10081050
public function _resetState(): void
10091051
{
1052+
$this->isAttributeTypeWebsiteSpecificCache = [];
10101053
$this->attributesPerSet = [];
10111054
$this->_attributeData = null;
1012-
foreach ($this->attributes ?? [] as $attributesGroupedByEntityTypeCode) {
1013-
foreach ($attributesGroupedByEntityTypeCode as $attribute) {
1014-
if ($attribute instanceof ResetAfterRequestInterface) {
1015-
$attribute->_resetState();
1055+
foreach ($this->attributes ?? [] as $attributesGroupedByWebsites) {
1056+
foreach ($attributesGroupedByWebsites ?? [] as $attributesGroupedByEntityTypeCode) {
1057+
foreach ($attributesGroupedByEntityTypeCode as $attribute) {
1058+
if ($attribute instanceof ResetAfterRequestInterface) {
1059+
$attribute->_resetState();
1060+
}
10161061
}
10171062
}
10181063
}

0 commit comments

Comments
 (0)