Change NextKey to create record in Module table if it doesnt exist
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
*/
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use App\Models\{Module,Record};
|
||||
|
||||
trait NextKey
|
||||
@@ -29,12 +32,22 @@ trait NextKey
|
||||
if (! defined(get_class($model).'::RECORD_ID'))
|
||||
throw new \Exception('Missing record_id const for '.get_class($model));
|
||||
|
||||
$mo = Module::where('name',$model::RECORD_ID)->firstOrFail();
|
||||
try {
|
||||
$mo = Module::where('name',$model::RECORD_ID)->firstOrFail();
|
||||
|
||||
} catch (ModelNotFoundException $e) {
|
||||
Log::critical(sprintf('Module [%s] not recorded, we\'ll create it.',$model::RECORD_ID),['model'=>$model->getAttributes()]);
|
||||
|
||||
$mo = new Module;
|
||||
$mo->name = $model::RECORD_ID;
|
||||
$mo->site_id = $model->site_id ?: config('SITE_SETUP')->id;
|
||||
$mo->save();
|
||||
}
|
||||
|
||||
if (! $mo->record) {
|
||||
$mo->record = new Record;
|
||||
$mo->record->module_id = $mo->id;
|
||||
$mo->record->site_id = 1; // @todo
|
||||
$mo->record->site_id = $model->site_id ?: config('SITE_SETUP')->id;
|
||||
}
|
||||
|
||||
$mo->record->id = $model->id;
|
||||
|
Reference in New Issue
Block a user