@@ -231,12 +231,38 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
231
231
$ this ->objectRelationProcessor ->validateDataIntegrity ($ this ->getMainTable (), $ object ->getData ());
232
232
if ($ object ->getId () !== null && (!$ this ->_useIsObjectNew || !$ object ->isObjectNew ())) {
233
233
$ condition = $ this ->_getWriteAdapter ()->quoteInto ($ this ->getIdFieldName () . '=? ' , $ object ->getId ());
234
- $ data = $ this ->_prepareDataForSave ($ object );
235
- unset($ data [$ this ->getIdFieldName ()]);
236
- $ this ->_getWriteAdapter ()->update ($ this ->getMainTable (), $ data , $ condition );
234
+ /**
235
+ * Not auto increment primary key support
236
+ */
237
+ if ($ this ->_isPkAutoIncrement ) {
238
+ $ data = $ this ->prepareDataForUpdate ($ object );
239
+ if (!empty ($ data )) {
240
+ $ this ->_getWriteAdapter ()->update ($ this ->getMainTable (), $ data , $ condition );
241
+ }
242
+ } else {
243
+ $ select = $ this ->_getWriteAdapter ()->select ()->from (
244
+ $ this ->getMainTable (),
245
+ [$ this ->getIdFieldName ()]
246
+ )->where (
247
+ $ condition
248
+ );
249
+ if ($ this ->_getWriteAdapter ()->fetchOne ($ select ) !== false ) {
250
+ $ data = $ this ->prepareDataForUpdate ($ object );
251
+ if (!empty ($ data )) {
252
+ $ this ->_getWriteAdapter ()->update ($ this ->getMainTable (), $ data , $ condition );
253
+ }
254
+ } else {
255
+ $ this ->_getWriteAdapter ()->insert (
256
+ $ this ->getMainTable (),
257
+ $ this ->_prepareDataForSave ($ object )
258
+ );
259
+ }
260
+ }
237
261
} else {
238
262
$ bind = $ this ->_prepareDataForSave ($ object );
239
- unset($ bind [$ this ->getIdFieldName ()]);
263
+ if ($ this ->_isPkAutoIncrement ) {
264
+ unset($ bind [$ this ->getIdFieldName ()]);
265
+ }
240
266
$ this ->_getWriteAdapter ()->insert ($ this ->getMainTable (), $ bind );
241
267
242
268
$ object ->setId ($ this ->_getWriteAdapter ()->lastInsertId ($ this ->getMainTable ()));
@@ -245,9 +271,10 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
245
271
$ object ->isObjectNew (false );
246
272
}
247
273
}
274
+
248
275
$ this ->unserializeFields ($ object );
249
276
$ this ->_afterSave ($ object );
250
- $ this -> entitySnapshot -> registerSnapshot ( $ object );
277
+
251
278
$ object ->afterSave ();
252
279
$ this ->processRelations ($ object );
253
280
}
0 commit comments