diff --git a/CHANGELOG.md b/CHANGELOG.md index 928461c3..3274c8cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. This projec ## Unreleased ### Fixed +- [#185](https://github.com/cloudcreativity/laravel-json-api/issues/185) +Rename adapter `store` method to `getStore` to avoid collisions with relation methods. - [#187](https://github.com/cloudcreativity/laravel-json-api/issues/187) Ensure hydration of Eloquent morph-many relationship works. - [#194](https://github.com/cloudcreativity/laravel-json-api/issues/194) diff --git a/docs/basics/adapters.md b/docs/basics/adapters.md index 33cfa1f7..3a284a90 100644 --- a/docs/basics/adapters.md +++ b/docs/basics/adapters.md @@ -396,6 +396,25 @@ class Adapter extends AbstractAdapter > The `morphMany` implementation currently has some limitations that we are hoping to resolve during our alpha and beta releases. If you have problems using it, please create an issue as this will help us out. +#### Customising Relation Method Names + +If you want to use a method name for your relation that is different than the JSON API field name, overload +the `methodForRelation` method on your adapter. For example, you would need to do this if the field name collides +with a method that already exists on the abstract adapter. + +The method can be overloaded as follows: + +```php +protected function methodForRelation($field) +{ + if ('myField' === $field) { + return 'myOtherMethodName'; + } + + return parent::methodForRelation($field); +} +``` + ## Custom Adapters Custom adapters can be used for any domain record that is not an Eloquent model. Adapters will work with this diff --git a/docs/upgrade.md b/docs/upgrade.md index fc0d618e..0967a411 100644 --- a/docs/upgrade.md +++ b/docs/upgrade.md @@ -36,6 +36,11 @@ public function read(StoreInterface $store, ValidatedRequest $request) } ``` +### Adapters + +The `store` method has been renamed to `getStore` to avoid collisions with JSON API relation names. This change +will only affect applications that are overloading internal adapter methods. + ### Has-Many The method signatures for the `sync` and `detach` methods in the JSON API Eloquent `HasMany` relation have been diff --git a/phpunit.xml b/phpunit.xml index 55d7dbec..3e6f925c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,7 +10,6 @@ processIsolation="false" stopOnError="false" stopOnFailure="false" - syntaxCheck="true" verbose="true" > diff --git a/src/Adapter/AbstractResourceAdapter.php b/src/Adapter/AbstractResourceAdapter.php index d907fc88..09b3a542 100644 --- a/src/Adapter/AbstractResourceAdapter.php +++ b/src/Adapter/AbstractResourceAdapter.php @@ -130,7 +130,7 @@ public function related($field) $relation->withFieldName($field); if ($relation instanceof StoreAwareInterface) { - $relation->withStore($this->store()); + $relation->withStore($this->getStore()); } return $relation; diff --git a/src/Eloquent/AbstractAdapter.php b/src/Eloquent/AbstractAdapter.php index 12694056..498c10f3 100644 --- a/src/Eloquent/AbstractAdapter.php +++ b/src/Eloquent/AbstractAdapter.php @@ -446,8 +446,20 @@ protected function findByIds(Builder $query, Collection $filters) * * @param Builder $query * @return Model + * @deprecated 1.0.0 use `searchOne`, renamed to avoid collisions with relation names. */ protected function first(Builder $query) + { + return $this->searchOne($query); + } + + /** + * Return the result for a search one query. + * + * @param Builder $query + * @return Model + */ + protected function searchOne($query) { return $query->first(); } @@ -457,8 +469,20 @@ protected function first(Builder $query) * * @param Builder $query * @return mixed + * @deprecated 1.0.0 use `searchAll`, renamed to avoid collisions with relation names. */ protected function all($query) + { + return $this->searchAll($query); + } + + /** + * Return the result for query that is not paginated. + * + * @param Builder $query + * @return mixed + */ + protected function searchAll($query) { return $query->get(); } diff --git a/src/Eloquent/AbstractManyRelation.php b/src/Eloquent/AbstractManyRelation.php index b0802ca7..fef8b5b7 100644 --- a/src/Eloquent/AbstractManyRelation.php +++ b/src/Eloquent/AbstractManyRelation.php @@ -43,7 +43,7 @@ public function query($record, EncodingParametersInterface $parameters) } $relation = $this->getRelation($record); - $adapter = $this->store()->adapterFor($relation->getModel()); + $adapter = $this->getStore()->adapterFor($relation->getModel()); if (!$adapter instanceof AbstractAdapter) { throw new RuntimeException('Expecting inverse adapter to be an Eloquent adapter.'); diff --git a/src/Eloquent/BelongsTo.php b/src/Eloquent/BelongsTo.php index e2fe4212..5a06743f 100644 --- a/src/Eloquent/BelongsTo.php +++ b/src/Eloquent/BelongsTo.php @@ -110,7 +110,7 @@ protected function findRelated(RelationshipInterface $relationship) { $identifier = $relationship->hasIdentifier() ? $relationship->getIdentifier() : null; - return $identifier ? $this->store()->find($identifier) : null; + return $identifier ? $this->getStore()->find($identifier) : null; } /** diff --git a/src/Eloquent/HasMany.php b/src/Eloquent/HasMany.php index e34b7acd..b8e6000c 100644 --- a/src/Eloquent/HasMany.php +++ b/src/Eloquent/HasMany.php @@ -166,7 +166,7 @@ protected function detach($relation, Collection $remove) protected function findRelated($record, RelationshipInterface $relationship) { $inverse = $this->getRelation($record)->getRelated(); - $related = $this->store()->findMany($relationship->getIdentifiers()); + $related = $this->getStore()->findMany($relationship->getIdentifiers()); $related = collect($related)->filter(function ($model) use ($inverse) { return $model instanceof $inverse; diff --git a/src/Store/StoreAwareTrait.php b/src/Store/StoreAwareTrait.php index b174508e..47f1ceb5 100644 --- a/src/Store/StoreAwareTrait.php +++ b/src/Store/StoreAwareTrait.php @@ -45,7 +45,7 @@ public function withStore(StoreInterface $store) /** * @return StoreInterface */ - protected function store() + protected function getStore() { if (!$this->store) { throw new RuntimeException('No store injected.');