@@ -181,10 +181,9 @@ protected function _prepareLoadSelect(array $selects)
181181 * @param \Magento\Catalog\Model\AbstractModel $object
182182 * @param AbstractAttribute $attribute
183183 * @param mixed $value
184- * @param int|null $valueId
185184 * @return $this
186185 */
187- protected function _saveAttributeValue ($ object , $ attribute , $ value, $ valueId = null )
186+ protected function _saveAttributeValue ($ object , $ attribute , $ value )
188187 {
189188 $ connection = $ this ->getConnection ();
190189 $ hasSingleStore = $ this ->_storeManager ->hasSingleStore ();
@@ -214,25 +213,28 @@ protected function _saveAttributeValue($object, $attribute, $value, $valueId = n
214213 );
215214 }
216215
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 );
236238 }
237239
238240 $ bind = $ this ->_prepareDataForTable ($ data , $ table );
@@ -339,7 +341,7 @@ protected function _insertAttribute($object, $attribute, $value)
339341 */
340342 protected function _updateAttribute ($ object , $ attribute , $ valueId , $ value )
341343 {
342- return $ this ->_saveAttributeValue ($ object , $ attribute , $ value, $ valueId );
344+ return $ this ->_saveAttributeValue ($ object , $ attribute , $ value );
343345 }
344346
345347 /**
0 commit comments