7
7
8
8
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
9
9
use Magento \Eav \Model \Entity \Type ;
10
+ use Magento \Eav \Model \ResourceModel \Attribute \Collection ;
10
11
use Magento \Eav \Model \ResourceModel \Attribute \DefaultEntityAttributes \ProviderInterface ;
11
12
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
13
use Magento \Framework \App \ObjectManager ;
@@ -28,17 +29,19 @@ class Config
28
29
/**#@+
29
30
* EAV cache ids
30
31
*/
31
- const ENTITIES_CACHE_ID = 'EAV_ENTITY_TYPES ' ;
32
- const ATTRIBUTES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES ' ;
33
- const ATTRIBUTES_CODES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES_CODES ' ;
32
+ public const ENTITIES_CACHE_ID = 'EAV_ENTITY_TYPES ' ;
33
+ public const ATTRIBUTES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES ' ;
34
+ public const ATTRIBUTES_CODES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES_CODES ' ;
34
35
/**#@-*/
35
36
36
37
/**
37
38
* Xml path to caching user defined eav attributes configuration.
38
39
*/
39
40
private const XML_PATH_CACHE_USER_DEFINED_ATTRIBUTES = 'dev/caching/cache_user_defined_attributes ' ;
40
41
41
- /**#@-*/
42
+ /**
43
+ * @var array|null
44
+ */
42
45
protected $ _entityTypeData ;
43
46
44
47
/**
@@ -574,6 +577,7 @@ public function getAttribute($entityType, $code)
574
577
* @param array $systemAttributes
575
578
* @return $this|bool|void
576
579
* @throws LocalizedException
580
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
577
581
*/
578
582
private function initSystemAttributes ($ entityType , $ systemAttributes )
579
583
{
@@ -582,8 +586,9 @@ private function initSystemAttributes($entityType, $systemAttributes)
582
586
if (!empty ($ this ->isSystemAttributesLoaded [$ entityTypeCode ])) {
583
587
return ;
584
588
}
585
-
586
- $ cacheKey = self ::ATTRIBUTES_CACHE_ID . '- ' . $ entityTypeCode . '-preload ' ;
589
+ $ attributeCollection = $ this ->_universalFactory ->create ($ entityType ->getEntityAttributeCollection ());
590
+ $ websiteId = $ attributeCollection instanceof Collection ? $ this ->getWebsiteId ($ attributeCollection ) : 0 ;
591
+ $ cacheKey = self ::ATTRIBUTES_CACHE_ID . '- ' . $ entityTypeCode . '- ' . $ websiteId . '-preload ' ;
587
592
if ($ this ->isCacheEnabled () && ($ attributes = $ this ->_cache ->load ($ cacheKey ))) {
588
593
$ attributes = $ this ->serializer ->unserialize ($ attributes );
589
594
if ($ attributes ) {
@@ -598,9 +603,7 @@ private function initSystemAttributes($entityType, $systemAttributes)
598
603
\Magento \Framework \Profiler::start ('EAV: ' . __METHOD__ , ['group ' => 'EAV ' , 'method ' => __METHOD__ ]);
599
604
600
605
/** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $attributes */
601
- $ attributes = $ this ->_universalFactory ->create (
602
- $ entityType ->getEntityAttributeCollection ()
603
- )->setEntityTypeFilter (
606
+ $ attributes = $ attributeCollection ->setEntityTypeFilter (
604
607
$ entityType
605
608
)->addFieldToFilter (
606
609
'attribute_code ' ,
@@ -762,16 +765,21 @@ public function getEntityAttributes($entityType, $object = null)
762
765
return $ this ->attributesPerSet [$ cacheKey ];
763
766
}
764
767
765
- $ attributesData = $ this ->isCacheEnabled () && ($ attributes = $ this ->_cache ->load ($ cacheKey ))
766
- ? $ this ->serializer ->unserialize ($ attributes )
767
- : null ;
768
+ $ attributeCollection = $ this ->_universalFactory ->create ($ entityType ->getEntityAttributeCollection ());
769
+ // If entity contains website-dependent attributes, the result should not be cached here.
770
+ // Website in collection is resolved by StoreManager which causes incorrect entity attributes caching when
771
+ // the entity is loaded from the backend first time after the cache cleanup.
772
+ $ isEntityWebsiteDependent = $ attributeCollection instanceof Collection;
773
+ $ attributesData = null ;
774
+ if ($ this ->isCacheEnabled () && !$ isEntityWebsiteDependent && ($ attributes = $ this ->_cache ->load ($ cacheKey ))) {
775
+ $ attributesData = $ this ->serializer ->unserialize ($ attributes );
776
+ }
768
777
769
778
$ attributes = [];
770
779
if ($ attributesData === null ) {
771
780
if ($ attributeSetId ) {
772
- $ attributesData = $ this ->_universalFactory ->create (
773
- $ entityType ->getEntityAttributeCollection ()
774
- )->setEntityTypeFilter (
781
+ $ attributeCollection = $ this ->_universalFactory ->create ($ entityType ->getEntityAttributeCollection ());
782
+ $ attributesData = $ attributeCollection ->setEntityTypeFilter (
775
783
$ entityType
776
784
)->setAttributeSetFilter (
777
785
$ attributeSetId
@@ -783,7 +791,7 @@ public function getEntityAttributes($entityType, $object = null)
783
791
$ attributesData = $ this ->_attributeData [$ entityType ->getEntityTypeCode ()];
784
792
}
785
793
786
- if ($ this ->isCacheEnabled ()) {
794
+ if ($ this ->isCacheEnabled () && ! $ isEntityWebsiteDependent ) {
787
795
$ this ->_cache ->save (
788
796
$ this ->serializer ->serialize ($ attributesData ),
789
797
$ cacheKey ,
@@ -960,4 +968,15 @@ private function initAttributesFromCache(Type $entityType)
960
968
}
961
969
return false ;
962
970
}
971
+
972
+ /**
973
+ * Returns website id.
974
+ *
975
+ * @param Collection $attributeCollection
976
+ * @return int
977
+ */
978
+ private function getWebsiteId (Collection $ attributeCollection ): int
979
+ {
980
+ return $ attributeCollection ->getWebsite () ? (int )$ attributeCollection ->getWebsite ()->getId () : 0 ;
981
+ }
963
982
}
0 commit comments