@@ -118,6 +118,13 @@ class Model extends BaseModel
118
118
*/
119
119
protected $ escape = [];
120
120
121
+ /**
122
+ * Primary Key value when inserting and useAutoIncrement is false.
123
+ *
124
+ * @var int|string|null
125
+ */
126
+ private $ tempPrimaryKeyValue ;
127
+
121
128
/**
122
129
* Builder method names that should not be used in the Model.
123
130
*
@@ -263,6 +270,13 @@ protected function doInsert(array $data)
263
270
$ escape = $ this ->escape ;
264
271
$ this ->escape = [];
265
272
273
+ // If $useAutoIncrement is false, add the primary key data.
274
+ if ($ this ->useAutoIncrement === false && $ this ->tempPrimaryKeyValue !== null ) {
275
+ $ data [$ this ->primaryKey ] = $ this ->tempPrimaryKeyValue ;
276
+
277
+ $ this ->tempPrimaryKeyValue = null ;
278
+ }
279
+
266
280
// Require non-empty primaryKey when
267
281
// not using auto-increment feature
268
282
if (! $ this ->useAutoIncrement && empty ($ data [$ this ->primaryKey ])) {
@@ -661,6 +675,10 @@ public function insert($data = null, bool $returnID = true)
661
675
}
662
676
}
663
677
678
+ if ($ this ->useAutoIncrement === false && isset ($ data [$ this ->primaryKey ])) {
679
+ $ this ->tempPrimaryKeyValue = $ data [$ this ->primaryKey ];
680
+ }
681
+
664
682
$ this ->escape = $ this ->tempData ['escape ' ] ?? [];
665
683
$ this ->tempData = [];
666
684
@@ -710,8 +728,13 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur
710
728
711
729
// Always grab the primary key otherwise updates will fail.
712
730
if (
713
- method_exists ($ data , 'toRawArray ' ) && (! empty ($ properties ) && ! empty ($ this ->primaryKey ) && ! in_array ($ this ->primaryKey , $ properties , true )
714
- && ! empty ($ data ->{$ this ->primaryKey }))
731
+ method_exists ($ data , 'toRawArray ' )
732
+ && (
733
+ ! empty ($ properties )
734
+ && ! empty ($ this ->primaryKey )
735
+ && ! in_array ($ this ->primaryKey , $ properties , true )
736
+ && ! empty ($ data ->{$ this ->primaryKey })
737
+ )
715
738
) {
716
739
$ properties [$ this ->primaryKey ] = $ data ->{$ this ->primaryKey };
717
740
}
0 commit comments