@@ -331,22 +331,31 @@ protected function convertData(): array
331331 $ mapping = $ this ->getOption ('autoMappingAlias ' , []);
332332 $ data = $ this ->getData ();
333333 $ item = [];
334+ $ together = [];
334335 foreach ($ properties as $ key => $ field ) {
335336 if (strpos ($ field , '-> ' )) {
336- $ fields = explode ('-> ' , $ field );
337- $ last = array_pop ($ fields );
338- $ target = $ this ->model ();
337+ $ fields = explode ('-> ' , $ field );
338+ $ together [] = current ($ fields );
339+ $ last = array_pop ($ fields );
340+ $ target = $ this ->model ();
339341 foreach ($ fields as $ attr ) {
340342 $ target = $ target ->$ attr ;
341343 }
342344 $ target ->$ last = $ data [$ key ];
343345 } elseif (is_int ($ key ) && isset ($ mapping [$ field ])) {
344346 [$ relation ] = explode ('-> ' , $ mapping [$ field ]);
347+ $ together [] = $ relation ;
345348 $ this ->model ()->$ relation ->$ field = $ data [$ field ];
346349 } else {
347350 $ item [$ field ] = $ data [is_int ($ key ) ? $ field : $ key ];
348351 }
349352 }
353+
354+ if (!empty ($ together )) {
355+ // 自动关联写入
356+ $ together = array_unique ($ together );
357+ $ this ->model ()->together ($ together );
358+ }
350359 return $ item ;
351360 }
352361
@@ -378,6 +387,10 @@ protected function validate(array $data = [], array $allow = []): array
378387 */
379388 public function save (): bool
380389 {
390+ if (!$ this ->getOption ('allowWrite ' , false )) {
391+ return false ;
392+ }
393+
381394 // 根据映射关系转换为实际模型数据
382395 $ data = $ this ->convertData ();
383396 // 处理自动时间字段数据
0 commit comments