@@ -181,9 +181,10 @@ 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
184185 * @return $this
185186 */
186- protected function _saveAttributeValue ($ object , $ attribute , $ value )
187+ protected function _saveAttributeValue ($ object , $ attribute , $ value, $ valueId = null )
187188 {
188189 $ connection = $ this ->getConnection ();
189190 $ hasSingleStore = $ this ->_storeManager ->hasSingleStore ();
@@ -213,14 +214,27 @@ protected function _saveAttributeValue($object, $attribute, $value)
213214 );
214215 }
215216
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- );
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+ );
236+ }
237+
224238 $ bind = $ this ->_prepareDataForTable ($ data , $ table );
225239
226240 if ($ attribute ->isScopeStore ()) {
@@ -325,15 +339,7 @@ protected function _insertAttribute($object, $attribute, $value)
325339 */
326340 protected function _updateAttribute ($ object , $ attribute , $ valueId , $ value )
327341 {
328- $ table = $ attribute ->getBackend ()->getTable ();
329- $ connection = $ this ->getConnection ();
330- $ connection ->update (
331- $ table ,
332- ['value ' => $ this ->_prepareValueForSave ($ value , $ attribute )],
333- sprintf ('%s=%d ' , $ connection ->quoteIdentifier ('value_id ' ), $ valueId )
334- );
335-
336- return $ this ;
342+ return $ this ->_saveAttributeValue ($ object , $ attribute , $ value , $ valueId );
337343 }
338344
339345 /**
0 commit comments