Tighten up EloquentAdapter

This commit is contained in:
Toby Zerner 2021-01-17 20:08:46 +10:30
parent 71d5114f0e
commit b57b6c352b
1 changed files with 22 additions and 12 deletions

View File

@ -11,10 +11,10 @@
namespace Tobyz\JsonApiServer\Adapter; namespace Tobyz\JsonApiServer\Adapter;
use Closure;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany; use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use InvalidArgumentException; use InvalidArgumentException;
@ -116,7 +116,15 @@ class EloquentAdapter implements AdapterInterface
public function setHasOne($model, HasOne $relationship, $related): void public function setHasOne($model, HasOne $relationship, $related): void
{ {
$this->getEloquentRelation($model, $relationship)->associate($related); $relation = $this->getEloquentRelation($model, $relationship);
if ($relation instanceof BelongsTo) {
if ($related === null) {
$relation->dissociate();
} else {
$relation->associate($related);
}
}
} }
public function save($model): void public function save($model): void
@ -126,7 +134,11 @@ class EloquentAdapter implements AdapterInterface
public function saveHasMany($model, HasMany $relationship, array $related): void public function saveHasMany($model, HasMany $relationship, array $related): void
{ {
$this->getEloquentRelation($model, $relationship)->sync(new Collection($related)); $relation = $this->getEloquentRelation($model, $relationship);
if ($relation instanceof BelongsToMany) {
$relation->sync(new Collection($related));
}
} }
public function delete($model): void public function delete($model): void
@ -134,11 +146,7 @@ class EloquentAdapter implements AdapterInterface
// For models that use the SoftDeletes trait, deleting the resource from // For models that use the SoftDeletes trait, deleting the resource from
// the API implies permanent deletion. Non-permanent deletion should be // the API implies permanent deletion. Non-permanent deletion should be
// achieved by manipulating a resource attribute. // achieved by manipulating a resource attribute.
if (method_exists($model, 'forceDelete')) {
$model->forceDelete(); $model->forceDelete();
} else {
$model->delete();
}
} }
public function filterByIds($query, array $ids): void public function filterByIds($query, array $ids): void
@ -158,7 +166,9 @@ class EloquentAdapter implements AdapterInterface
public function filterByHasOne($query, HasOne $relationship, array $ids): void public function filterByHasOne($query, HasOne $relationship, array $ids): void
{ {
$relation = $this->getEloquentRelation($query->getModel(), $relationship); $relation = $this->getEloquentRelation($query->getModel(), $relationship);
$column = $relation instanceof HasOneThrough ? $relation->getQualifiedParentKeyName() : $relation->getQualifiedForeignKeyName(); $column = $relation instanceof HasOneThrough
? $relation->getQualifiedParentKeyName()
: $relation->getQualifiedForeignKeyName();
$query->whereIn($column, $ids); $query->whereIn($column, $ids);
} }
@ -199,9 +209,9 @@ class EloquentAdapter implements AdapterInterface
$query = $relation->getQuery(); $query = $relation->getQuery();
if (is_array($scope)) { if (is_array($scope)) {
// Eloquent doesn't support polymorphic loading constraints, // TODO: since https://github.com/laravel/framework/pull/35190
// so for now we just won't do anything. // was merged, we can now apply loading constraints to
// https://github.com/laravel/framework/pull/35190 // polymorphic relationships.
} else { } else {
$scope($query); $scope($query);
} }