From c16561eddb23a95c6d37408562e80866a4cff48b Mon Sep 17 00:00:00 2001 From: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> Date: Tue, 22 Feb 2022 23:33:35 +0000 Subject: [PATCH] Set model attributes instead to avoid eloquent guards Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> --- actions/RestController.php | 4 ++- classes/AbstractRepository.php | 54 +++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/actions/RestController.php b/actions/RestController.php index db531db..5f8b651 100644 --- a/actions/RestController.php +++ b/actions/RestController.php @@ -187,7 +187,9 @@ protected function validateRequest($requestMethod) $request->setController($this->controller); }); - return app()->make($requestClass)->validated(); + app()->make($requestClass); + + return $requestData; } return $this->controller->restValidate($requestData); diff --git a/classes/AbstractRepository.php b/classes/AbstractRepository.php index d1b843f..aa9f295 100644 --- a/classes/AbstractRepository.php +++ b/classes/AbstractRepository.php @@ -8,6 +8,7 @@ use Igniter\Flame\Exception\SystemException; use Igniter\Flame\Traits\EventEmitter; use Illuminate\Contracts\Container\Container; +use Illuminate\Support\Facades\DB; class AbstractRepository { @@ -29,6 +30,11 @@ class AbstractRepository */ protected $modelClass; + /** + * @var array List of prepared models that require saving. + */ + protected $modelsToSave = []; + public function find(int $id, array $attributes = ['*']) { $model = $this->createModel(); @@ -70,15 +76,20 @@ public function paginate($perPage = null, $page = null, $pageName = 'page', $col return $query->paginate($perPage, $page, $columns, $pageName); } - public function create($model, array $attributes) + public function create(Model $model, array $attributes) { $this->fireSystemEvent('api.repository.beforeCreate', [$model, $attributes]); - $model->fill($attributes); + $this->modelsToSave = []; + $this->setModelAttributes($model, $attributes); - $created = $model->save(); + DB::transaction(function () { + foreach ($this->modelsToSave as $modelToSave) { + $modelToSave->save(); + } + }); - $this->fireSystemEvent('api.repository.afterCreate', [$model, $created]); + $this->fireSystemEvent('api.repository.afterCreate', [$model, TRUE]); return $model; } @@ -92,11 +103,16 @@ public function update($id, array $attributes = []) $this->fireSystemEvent('api.repository.beforeUpdate', [$model, $attributes]); - $model->fill($attributes); + $this->modelsToSave = []; + $this->setModelAttributes($model, $attributes); - $updated = $model->save(); + DB::transaction(function () { + foreach ($this->modelsToSave as $modelToSave) { + $modelToSave->save(); + } + }); - $this->fireSystemEvent('api.repository.afterUpdate', [$model, $updated]); + $this->fireSystemEvent('api.repository.afterUpdate', [$model, TRUE]); return $model; } @@ -201,4 +217,28 @@ protected function prepareQuery($model) protected function extendQuery($query) { } + + protected function setModelAttributes($model, $saveData) + { + if (!is_array($saveData) || !$model) { + return; + } + + $this->modelsToSave[] = $model; + + $singularTypes = ['belongsTo', 'hasOne', 'morphOne']; + foreach ($saveData as $attribute => $value) { + $isNested = ($attribute == 'pivot' || ( + $model->hasRelation($attribute) && + in_array($model->getRelationType($attribute), $singularTypes) + )); + + if ($isNested && is_array($value) && $model->{$attribute}) { + $this->setModelAttributes($model->{$attribute}, $value); + } + elseif (!starts_with($attribute, '_')) { + $model->{$attribute} = $value; + } + } + } }