@@ -85,7 +85,6 @@ public function __construct(array | object $data = [], ?Model $model = null)
85
85
'mapping ' => $ options ['mapping ' ] ?? [],
86
86
'strict ' => $ options ['strict ' ] ?? true ,
87
87
'bind_attr ' => $ options ['bind_attr ' ] ?? [],
88
- 'auto_insert ' => $ options ['auto_insert ' ] ?? [],
89
88
'auto_relation ' => $ options ['auto_relation ' ] ?? [],
90
89
'relation_keys ' => $ options ['relation_keys ' ] ?? [],
91
90
];
@@ -827,10 +826,8 @@ public function save(array | object $data = [], $where = []): bool
827
826
// 自动时间戳处理
828
827
$ this ->autoDateTime ($ data , $ isUpdate );
829
828
830
- if (!$ isUpdate ) {
831
- // 自动写入数据
832
- $ this ->autoInsertData ($ data );
833
- }
829
+ // 自动写入数据
830
+ $ this ->autoWriteData ($ data , $ isUpdate );
834
831
835
832
$ model = $ this ->model ();
836
833
if ($ model instanceof Model) {
@@ -906,23 +903,22 @@ protected function autoDateTime(array &$data, bool $update)
906
903
* 字段自动写入.
907
904
*
908
905
* @param array $data 数据
906
+ * @param bool $isUpdate 是否更新
909
907
* @return void
910
908
*/
911
- protected function autoInsertData (array &$ data )
912
- {
913
- $ autoInsert = $ this ->getOption ('auto_insert ' , []);
914
- if (!empty ($ autoInsert )) {
915
- foreach ($ autoInsert as $ name => $ val ) {
916
- $ field = is_string ($ name ) ? $ name : $ val ;
917
- if (!isset ($ data [$ field ])) {
918
- if ($ val instanceof Closure) {
919
- $ value = $ val ($ this );
920
- } else {
921
- $ value = is_string ($ name ) ? $ val : $ this ->setWithAttr ($ field , null , $ data );
922
- }
923
- $ data [$ field ] = $ value ;
924
- $ this ->setData ($ field , $ value );
909
+ protected function autoWriteData (array &$ data , bool $ isUpdate )
910
+ {
911
+ $ auto = $ this ->getOption ($ isUpdate ? 'auto_update ' : 'auto_insert ' , []);
912
+ foreach ($ auto as $ name => $ val ) {
913
+ $ field = is_string ($ name ) ? $ name : $ val ;
914
+ if (!isset ($ data [$ field ])) {
915
+ if ($ val instanceof Closure) {
916
+ $ value = $ val ($ this );
917
+ } else {
918
+ $ value = is_string ($ name ) ? $ val : $ this ->setWithAttr ($ field , null , $ data );
925
919
}
920
+ $ data [$ field ] = $ value ;
921
+ $ this ->setData ($ field , $ value );
926
922
}
927
923
}
928
924
}
0 commit comments