@@ -509,21 +509,32 @@ protected function associateCategories($categoryClass, $property)
509
509
})
510
510
->first ();
511
511
512
+ // If the process has a set category, we need to verify the "Uncategorized"
513
+ // category exists for this model and if it doesn't, recreate it
514
+ $ uncategorizedCategory = function () use ($ categoryClass ) {
515
+ return $ categoryClass ::where ('name ' , 'Uncategorized ' )->firstOrCreate ([
516
+ 'name ' => __ ('Uncategorized ' ),
517
+ 'status ' => 'ACTIVE ' ,
518
+ 'is_system ' => false ,
519
+ ]);
520
+ };
521
+
512
522
// If a template is being used and an associated category is present, add that category to the collection.
513
523
// Otherwise, if the collection is empty and there's an uncategorized reference, add the uncategorized category.
514
524
if ($ isTemplate && isset ($ this ->model ->process_category_id )) {
515
525
if ($ this ->getReference ('uncategorized-category ' )) {
516
- $ categories ->push ($ categoryClass :: where ( ' name ' , ' Uncategorized ' )-> firstOrFail ());
526
+ $ categories ->push ($ uncategorizedCategory ());
517
527
} else {
518
- $ categorFind = $ categoryClass ::find ($ this ->model ->process_category_id );
519
- if (! $ categorFind ) {
520
- $ categorFind = $ categoryClass:: where ( ' name ' , ' Uncategorized ' )-> firstOrFail ();
521
- \ Log::debug ( $ categoryClass . ' ID: ' . $ this ->model ->process_category_id . ' not found. Changing category toUncategorize. ' );
528
+ $ foundCategory = $ categoryClass ::find ($ this ->model ->process_category_id );
529
+
530
+ if (! $ foundCategory instanceof $ categoryClass) {
531
+ Log::warning ( " Import/Export: Unable to find " . $ categoryClass ::class. " with id { $ this ->model ->process_category_id } , updated to 'Uncategorized'. " );
522
532
}
523
- $ categories ->push ($ categorFind );
533
+
534
+ $ categories ->push ($ foundCategory ?? $ uncategorizedCategory ());
524
535
}
525
536
} elseif ($ categories ->empty () && $ this ->getReference ('uncategorized-category ' )) {
526
- $ categories ->push ($ categoryClass :: where ( ' name ' , ' Uncategorized ' )-> firstOrFail ());
537
+ $ categories ->push ($ uncategorizedCategory ());
527
538
}
528
539
529
540
$ categoriesString = $ categories ->map (fn ($ c ) => $ c ->id )->unique ()->join (', ' );
0 commit comments