6
6
7
7
namespace Magento \Catalog \Model \Attribute ;
8
8
9
+ use Magento \Catalog \Model \AbstractModel ;
10
+ use Magento \Framework \DataObject ;
9
11
use Magento \Framework \EntityManager \MetadataPool ;
10
12
use Magento \Eav \Api \AttributeRepositoryInterface as AttributeRepository ;
11
13
use Magento \Framework \Api \SearchCriteriaBuilder ;
@@ -75,7 +77,7 @@ public function __construct(
75
77
* Whether attribute value is overridden in specific store
76
78
*
77
79
* @param string $entityType
78
- * @param \Magento\Catalog\Model\ AbstractModel $entity
80
+ * @param AbstractModel $entity
79
81
* @param string $attributeCode
80
82
* @param int|string $storeId
81
83
* @return bool
@@ -85,39 +87,41 @@ public function containsValue($entityType, $entity, $attributeCode, $storeId)
85
87
if ((int )$ storeId === Store::DEFAULT_STORE_ID ) {
86
88
return false ;
87
89
}
88
- if (!isset ($ this ->attributesValues [$ storeId ])) {
90
+ $ values = $ this ->getAttributesValues ($ entityType , $ entity );
91
+
92
+ if (!isset ($ values [$ storeId ])) {
89
93
$ this ->initAttributeValues ($ entityType , $ entity , (int )$ storeId );
94
+ $ values = $ this ->getAttributesValues ($ entityType , $ entity );
90
95
}
91
96
92
- return isset ($ this ->attributesValues [$ storeId ])
93
- && array_key_exists ($ attributeCode , $ this ->attributesValues [$ storeId ]);
97
+ return isset ($ values [$ storeId ]) && array_key_exists ($ attributeCode , $ values [$ storeId ]);
94
98
}
95
99
96
100
/**
97
101
* Get attribute default values
98
102
*
99
103
* @param string $entityType
100
- * @param \Magento\Catalog\Model\ AbstractModel $entity
104
+ * @param AbstractModel $entity
101
105
* @return array
102
106
*
103
107
* @deprecated 101.0.0
104
108
*/
105
109
public function getDefaultValues ($ entityType , $ entity )
106
110
{
107
- if ($ this ->attributesValues === null ) {
111
+ $ values = $ this ->getAttributesValues ($ entityType , $ entity );
112
+ if (!isset ($ values [Store::DEFAULT_STORE_ID ])) {
108
113
$ this ->initAttributeValues ($ entityType , $ entity , (int )$ entity ->getStoreId ());
114
+ $ values = $ this ->getAttributesValues ($ entityType , $ entity );
109
115
}
110
116
111
- return isset ($ this ->attributesValues [Store::DEFAULT_STORE_ID ])
112
- ? $ this ->attributesValues [Store::DEFAULT_STORE_ID ]
113
- : [];
117
+ return $ values [Store::DEFAULT_STORE_ID ] ?? [];
114
118
}
115
119
116
120
/**
117
121
* Init attribute values.
118
122
*
119
123
* @param string $entityType
120
- * @param \Magento\Catalog\Model\ AbstractModel $entity
124
+ * @param AbstractModel $entity
121
125
* @param int $storeId
122
126
* @throws \Magento\Framework\Exception\LocalizedException
123
127
* @return void
@@ -128,6 +132,7 @@ private function initAttributeValues($entityType, $entity, $storeId)
128
132
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
129
133
$ attributeTables = [];
130
134
if ($ metadata ->getEavEntityType ()) {
135
+ $ entityId = $ entity ->getData ($ metadata ->getLinkField ());
131
136
foreach ($ this ->getAttributes ($ entityType ) as $ attribute ) {
132
137
if (!$ attribute ->isStatic ()) {
133
138
$ attributeTables [$ attribute ->getBackend ()->getTable ()][] = $ attribute ->getAttributeId ();
@@ -146,7 +151,7 @@ private function initAttributeValues($entityType, $entity, $storeId)
146
151
'a.attribute_id = t.attribute_id ' ,
147
152
['attribute_code ' => 'a.attribute_code ' ]
148
153
)
149
- ->where ($ metadata ->getLinkField () . ' = ? ' , $ entity -> getData ( $ metadata -> getLinkField ()) )
154
+ ->where ($ metadata ->getLinkField () . ' = ? ' , $ entityId )
150
155
->where ('t.attribute_id IN (?) ' , $ attributeCodes )
151
156
->where ('t.store_id IN (?) ' , $ storeIds );
152
157
$ selects [] = $ select ;
@@ -157,9 +162,14 @@ private function initAttributeValues($entityType, $entity, $storeId)
157
162
\Magento \Framework \DB \Select::SQL_UNION_ALL
158
163
);
159
164
$ attributes = $ metadata ->getEntityConnection ()->fetchAll ((string )$ unionSelect );
165
+ $ values = array_merge (
166
+ $ this ->getAttributesValues ($ entityType , $ entity ),
167
+ array_fill_keys ($ storeIds , [])
168
+ );
160
169
foreach ($ attributes as $ attribute ) {
161
- $ this -> attributesValues [$ attribute ['store_id ' ]][$ attribute ['attribute_code ' ]] = $ attribute ['value ' ];
170
+ $ values [$ attribute ['store_id ' ]][$ attribute ['attribute_code ' ]] = $ attribute ['value ' ];
162
171
}
172
+ $ this ->setAttributesValues ($ entityType , $ entity , $ values );
163
173
}
164
174
}
165
175
@@ -188,12 +198,50 @@ private function getAttributes($entityType)
188
198
}
189
199
190
200
/**
191
- * Clear attributes values cache
201
+ * Clear entity attributes values cache
202
+ *
203
+ * @param string $entityType
204
+ * @param DataObject $entity
205
+ * @return void
206
+ * @throws \Exception
207
+ */
208
+ public function clearAttributesValues (string $ entityType , DataObject $ entity ): void
209
+ {
210
+ if (isset ($ this ->attributesValues [$ entityType ])) {
211
+ $ metadata = $ this ->metadataPool ->getMetadata ($ entityType );
212
+ $ entityId = $ entity ->getData ($ metadata ->getLinkField ());
213
+ unset($ this ->attributesValues [$ entityType ][$ entityId ]);
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Get entity attributes values from cache
192
219
*
220
+ * @param string $entityType
221
+ * @param DataObject $entity
222
+ * @return array
223
+ * @throws \Exception
224
+ */
225
+ private function getAttributesValues (string $ entityType , DataObject $ entity ): array
226
+ {
227
+ $ metadata = $ this ->metadataPool ->getMetadata ($ entityType );
228
+ $ entityId = $ entity ->getData ($ metadata ->getLinkField ());
229
+ return $ this ->attributesValues [$ entityType ][$ entityId ] ?? [];
230
+ }
231
+
232
+ /**
233
+ * Set entity attributes values into cache
234
+ *
235
+ * @param string $entityType
236
+ * @param DataObject $entity
237
+ * @param array $values
193
238
* @return void
239
+ * @throws \Exception
194
240
*/
195
- public function clearAttributeValues ( ): void
241
+ private function setAttributesValues ( string $ entityType , DataObject $ entity , array $ values ): void
196
242
{
197
- $ this ->attributesValues = null ;
243
+ $ metadata = $ this ->metadataPool ->getMetadata ($ entityType );
244
+ $ entityId = $ entity ->getData ($ metadata ->getLinkField ());
245
+ $ this ->attributesValues [$ entityType ][$ entityId ] = $ values ;
198
246
}
199
247
}
0 commit comments