Skip to content

Commit

Permalink
Set model attributes instead to avoid eloquent guards
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Feb 22, 2022
1 parent 261403b commit c16561e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
4 changes: 3 additions & 1 deletion actions/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 47 additions & 7 deletions classes/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit c16561e

Please sign in to comment.