@@ -181,10 +181,9 @@ protected function _prepareLoadSelect(array $selects)
181
181
* @param \Magento\Catalog\Model\AbstractModel $object
182
182
* @param AbstractAttribute $attribute
183
183
* @param mixed $value
184
- * @param int|null $valueId
185
184
* @return $this
186
185
*/
187
- protected function _saveAttributeValue ($ object , $ attribute , $ value, $ valueId = null )
186
+ protected function _saveAttributeValue ($ object , $ attribute , $ value )
188
187
{
189
188
$ connection = $ this ->getConnection ();
190
189
$ hasSingleStore = $ this ->_storeManager ->hasSingleStore ();
@@ -214,25 +213,28 @@ protected function _saveAttributeValue($object, $attribute, $value, $valueId = n
214
213
);
215
214
}
216
215
217
- if ($ valueId > 0 ) {
218
- $ data = new \Magento \Framework \DataObject (
219
- [
220
- 'value_id ' => $ valueId ,
221
- 'attribute_id ' => $ attribute ->getAttributeId (),
222
- 'store_id ' => $ storeId ,
223
- $ entityIdField => $ object ->getData ($ entityIdField ),
224
- 'value ' => $ this ->_prepareValueForSave ($ value , $ attribute ),
225
- ]
226
- );
227
- } else {
228
- $ data = new \Magento \Framework \DataObject (
229
- [
230
- 'attribute_id ' => $ attribute ->getAttributeId (),
231
- 'store_id ' => $ storeId ,
232
- $ entityIdField => $ object ->getData ($ entityIdField ),
233
- 'value ' => $ this ->_prepareValueForSave ($ value , $ attribute ),
234
- ]
235
- );
216
+ $ data = new \Magento \Framework \DataObject (
217
+ [
218
+ 'attribute_id ' => $ attribute ->getAttributeId (),
219
+ 'store_id ' => $ storeId ,
220
+ $ entityIdField => $ object ->getData ($ entityIdField ),
221
+ 'value ' => $ this ->_prepareValueForSave ($ value , $ attribute ),
222
+ ]
223
+ );
224
+
225
+ $ entity = $ attribute ->getEntity ();
226
+ $ row = $ this ->getAttributeRow ($ entity , $ object , $ attribute );
227
+ $ whereArr = [];
228
+ foreach ($ row as $ field => $ value ) {
229
+ $ whereArr [] = $ connection ->quoteInto ($ field . '=? ' , $ value );
230
+ }
231
+ $ where = implode (' AND ' , $ whereArr );
232
+ $ select = $ connection ->select ()->from ($ table , ['value_id ' , 'value ' ])->where ($ where );
233
+ $ origRow = $ connection ->fetchRow ($ select );
234
+ $ origValueId = $ origRow ['value_id ' ] ?? false ;
235
+
236
+ if ($ origValueId > 0 ) {
237
+ $ data ->setData ('value_id ' , $ origValueId );
236
238
}
237
239
238
240
$ bind = $ this ->_prepareDataForTable ($ data , $ table );
@@ -339,7 +341,7 @@ protected function _insertAttribute($object, $attribute, $value)
339
341
*/
340
342
protected function _updateAttribute ($ object , $ attribute , $ valueId , $ value )
341
343
{
342
- return $ this ->_saveAttributeValue ($ object , $ attribute , $ value, $ valueId );
344
+ return $ this ->_saveAttributeValue ($ object , $ attribute , $ value );
343
345
}
344
346
345
347
/**
0 commit comments