fix some bugs

This commit is contained in:
Toby Zerner 2019-11-21 15:15:24 +10:30
parent 91c0387b5b
commit fab1a86925
4 changed files with 24 additions and 22 deletions

View File

@ -15,7 +15,6 @@ 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 InvalidArgumentException; use InvalidArgumentException;
use Tobyz\JsonApiServer\Schema\Attribute; use Tobyz\JsonApiServer\Schema\Attribute;
use Tobyz\JsonApiServer\Schema\HasMany; use Tobyz\JsonApiServer\Schema\HasMany;
@ -65,7 +64,7 @@ class EloquentAdapter implements AdapterInterface
public function count($query): int public function count($query): int
{ {
return $query->count(); return $query->getQuery()->getCountForPagination();
} }
public function getId($model): string public function getId($model): string
@ -80,20 +79,22 @@ class EloquentAdapter implements AdapterInterface
public function getHasOne($model, HasOne $relationship, bool $linkage) public function getHasOne($model, HasOne $relationship, bool $linkage)
{ {
$relation = $this->getEloquentRelation($model, $relationship);
// If it's a belongs-to relationship and we only need to get the ID, // If it's a belongs-to relationship and we only need to get the ID,
// then we don't have to actually load the relation because the ID is // then we don't have to actually load the relation because the ID is
// stored in a column directly on the model. We will mock up a related // stored in a column directly on the model. We will mock up a related
// model with the value of the ID filled. // model with the value of the ID filled.
if ($linkage && $relation instanceof BelongsTo) { if ($linkage) {
if ($key = $model->{$relation->getForeignKeyName()}) { $relation = $this->getEloquentRelation($model, $relationship);
$related = $relation->getRelated();
return $related->newInstance()->forceFill([$related->getKeyName() => $key]); if ($relation instanceof BelongsTo) {
if ($key = $model->{$relation->getForeignKeyName()}) {
$related = $relation->getRelated();
return $related->newInstance()->forceFill([$related->getKeyName() => $key]);
}
return null;
} }
return null;
} }
return $this->getRelationValue($model, $relationship); return $this->getRelationValue($model, $relationship);

View File

@ -16,6 +16,7 @@ use Tobyz\JsonApiServer\Exception\BadRequestException;
use Tobyz\JsonApiServer\JsonApi; use Tobyz\JsonApiServer\JsonApi;
use Tobyz\JsonApiServer\ResourceType; use Tobyz\JsonApiServer\ResourceType;
use Tobyz\JsonApiServer\Schema\Relationship; use Tobyz\JsonApiServer\Schema\Relationship;
use function Tobyz\JsonApiServer\evaluate;
use function Tobyz\JsonApiServer\run_callbacks; use function Tobyz\JsonApiServer\run_callbacks;
/** /**
@ -95,6 +96,7 @@ trait IncludesData
if ( if (
! $field instanceof Relationship ! $field instanceof Relationship
|| (! $field->isLinkage() && ! isset($include[$name])) || (! $field->isLinkage() && ! isset($include[$name]))
|| $field->isVisible() === false
) { ) {
continue; continue;
} }
@ -111,12 +113,12 @@ trait IncludesData
$adapter->load($models, $nextRelationshipPath, $scope, $field->isLinkage()); $adapter->load($models, $nextRelationshipPath, $scope, $field->isLinkage());
} }
}
if (isset($include[$name]) && is_string($type = $field->getType())) { if (isset($include[$name]) && is_string($type = $field->getType())) {
$relatedResource = $this->api->getResource($type); $relatedResource = $this->api->getResource($type);
$this->loadRelationshipsAtLevel($models, $nextRelationshipPath, $relatedResource, $include[$name] ?? [], $request); $this->loadRelationshipsAtLevel($models, $nextRelationshipPath, $relatedResource, $include[$name] ?? [], $request);
}
} }
} }
} }

View File

@ -11,6 +11,7 @@
namespace Tobyz\JsonApiServer\Handler; namespace Tobyz\JsonApiServer\Handler;
use Illuminate\Support\Arr;
use JsonApiPhp\JsonApi as Structure; use JsonApiPhp\JsonApi as Structure;
use JsonApiPhp\JsonApi\Link\LastLink; use JsonApiPhp\JsonApi\Link\LastLink;
use JsonApiPhp\JsonApi\Link\NextLink; use JsonApiPhp\JsonApi\Link\NextLink;
@ -57,13 +58,11 @@ class Index implements RequestHandlerInterface
$include = $this->getInclude($request); $include = $this->getInclude($request);
$this->filter($query, $request); [$offset, $limit] = $this->paginate($query, $request);
$this->sort($query, $request); $this->sort($query, $request);
$this->filter($query, $request);
$total = $schema->isCountable() ? $adapter->count($query) : null; $total = $schema->isCountable() ? $adapter->count($query) : null;
[$offset, $limit] = $this->paginate($query, $request);
$models = $adapter->get($query); $models = $adapter->get($query);
$this->loadRelationships($models, $include, $request); $this->loadRelationships($models, $include, $request);
@ -107,7 +106,7 @@ class Index implements RequestHandlerInterface
} }
} }
$queryString = http_build_query($queryParams); $queryString = Arr::query($queryParams);
return $selfUrl.($queryString ? '?'.$queryString : ''); return $selfUrl.($queryString ? '?'.$queryString : '');
} }

View File

@ -101,7 +101,7 @@ final class Serializer
$value = $this->attribute($field, $resource, $model); $value = $this->attribute($field, $resource, $model);
} elseif ($field instanceof Schema\Relationship) { } elseif ($field instanceof Schema\Relationship) {
$isIncluded = isset($include[$name]); $isIncluded = isset($include[$name]);
$relationshipInclude = $isIncluded ? ($relationshipInclude[$name] ?? []) : null; $relationshipInclude = $isIncluded ? ($include[$name] ?? []) : null;
$links = $this->relationshipLinks($field, $url); $links = $this->relationshipLinks($field, $url);
$meta = $this->meta($field->getMeta(), $model); $meta = $this->meta($field->getMeta(), $model);
$members = array_merge($links, $meta); $members = array_merge($links, $meta);
@ -160,7 +160,7 @@ final class Serializer
{ {
$included = $include !== null; $included = $include !== null;
$model = ($included && $getCallback = $field->getGetCallback()) $model = ($getCallback = $field->getGetCallback())
? $getCallback($model, $this->request) ? $getCallback($model, $this->request)
: $resource->getAdapter()->getHasOne($model, $field, ! $included); : $resource->getAdapter()->getHasOne($model, $field, ! $included);
@ -179,7 +179,7 @@ final class Serializer
{ {
$included = $include !== null; $included = $include !== null;
$models = ($included && $getCallback = $field->getGetCallback()) $models = ($getCallback = $field->getGetCallback())
? $getCallback($model, $this->request) ? $getCallback($model, $this->request)
: $resource->getAdapter()->getHasMany($model, $field, ! $included); : $resource->getAdapter()->getHasMany($model, $field, ! $included);