@@ -302,7 +302,14 @@ protected function relationsAfterInsert(array $eventData): array
302302 }
303303
304304 foreach ($ this ->relations as $ relationObject ) {
305- foreach ($ relationObject ->getData () as $ row ) {
305+ $ data = $ relationObject ->getData ();
306+
307+ // Skip if data is null or empty - nothing to insert
308+ if ($ data === [null ] || $ data === []) {
309+ continue ;
310+ }
311+
312+ foreach ($ data as $ row ) {
306313 $ row = $ this ->transformDataToArray ($ row , 'insert ' );
307314 $ result = $ relationObject ->applyWith ()->model ->insert (array_merge ($ row , [
308315 $ relationObject ->foreignKey => $ eventData [$ this ->primaryKey ],
@@ -349,7 +356,14 @@ protected function relationsAfterUpdate(array $eventData): array
349356 }
350357
351358 foreach ($ this ->relations as $ relationObject ) {
352- foreach ($ relationObject ->getData () as $ row ) {
359+ $ data = $ relationObject ->getData ();
360+
361+ // Skip if data is null or empty - nothing to update
362+ if ($ data === [null ] || $ data === []) {
363+ continue ;
364+ }
365+
366+ foreach ($ data as $ row ) {
353367 $ row = $ this ->transformDataToArray ($ row , 'insert ' );
354368
355369 foreach ($ eventData [$ this ->primaryKey ] as $ id ) {
@@ -432,11 +446,9 @@ protected function getDataForRelationById(int|string $id, Relation $relation, st
432446
433447 $ relation ->applyWith ()->applyRelation ($ id , $ this ->primaryKey )->applyConditions ();
434448
435- $ results = in_array ($ relation ->type , [RelationTypes::hasOne, RelationTypes::belongsTo], true ) ?
436- $ relation ->model ->first () :
437- $ relation ->model ->findAll ();
438-
439- return $ relation ->filterResults ($ results , $ this ->tempReturnType );
449+ return in_array ($ relation ->type , [RelationTypes::hasOne, RelationTypes::belongsTo], true ) ?
450+ $ relation ->filterResult ($ relation ->model ->first (), $ this ->tempReturnType ) :
451+ $ relation ->filterResults ($ relation ->model ->findAll (), $ this ->tempReturnType );
440452 }
441453
442454 /**
0 commit comments