8000 [Refactor] Always pass to-many queries to the inverse adapter · AxonDivisionDev/laravel-json-api@242eddd · GitHub
[go: up one dir, main page]

Skip to content

Commit 242eddd

Browse files
committed
[Refactor] Always pass to-many queries to the inverse adapter
This is now needed as the inverse adapter might be configured with default include, sort etc paths. If we do not pass it over, these defaults will not be applied.
1 parent 4385f89 commit 242eddd

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

src/Eloquent/AbstractManyRelation.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@ abstract class AbstractManyRelation extends AbstractRelation implements HasManyA
3636
*/
3737
public function query($record, EncodingParametersInterface $parameters)
3838
{
39-
/** If we do not need to pass to the inverse adapter, we can just return the whole relationship. */
40-
if (!$this->requiresInverseAdapter($record, $parameters)) {
41-
return $record->{$this->key};
42-
}
43-
4439
$relation = $this->getRelation($record);
4540

46-
return $this->adapterFor($relation)->queryRelation($relation, $parameters);
41+
return $this->adapterFor($relation)->queryToMany($relation, $parameters);
4742
}
4843

4944
/**

src/Eloquent/MorphHasMany.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
use CloudCreativity\LaravelJsonApi\Contracts\Adapter\HasManyAdapterInterface;
2121
use CloudCreativity\LaravelJsonApi\Contracts\Object\RelationshipInterface;
22+
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PageInterface;
2223
use CloudCreativity\LaravelJsonApi\Contracts\Store\StoreAwareInterface;
2324
use CloudCreativity\LaravelJsonApi\Contracts\Store\StoreInterface;
25+
use Illuminate\Pagination\AbstractPaginator;
2426
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
2527

2628
/**
@@ -80,7 +82,8 @@ public function query($record, EncodingParametersInterface $parameters)
8082
$all = collect();
8183

8284
foreach ($this->adapters as $adapter) {
83-
$all = $all->merge($adapter->query($record, $parameters));
85+
$results = $adapter->query($record, $parameters);
86+
$all = $all->merge($this->extractItems($results));
8487
}
8588

8689
return $all;
@@ -94,7 +97,8 @@ public function relationship($record, EncodingParametersInterface $parameters)
9497
$all = collect();
9598

9699
foreach ($this->adapters as $adapter) {
97-
$all = $all->merge($adapter->relationship($record, $parameters));
100+
$results = $adapter->relationship($record, $parameters);
101+
$all = $all->merge($this->extractItems($results));
98102
}
99103

100104
return $all;
@@ -148,4 +152,21 @@ public function remove($record, RelationshipInterface $relationship, EncodingPar
148152
return $record;
149153
}
150154

155+
/**
156+
* @param $results
157+
* @return array|iterable
158+
*/
159+
protected function extractItems($results)
160+
{
161+
if ($results instanceof PageInterface) {
162+
$results = $results->getData();
163+
}
164+
165+
if ($results instanceof AbstractPaginator) {
166+
$results = $results->all();
167+
}
168+
169+
return $results;
170+
}
171+
151172
}

tests/lib/Integration/Eloquent/MorphToManyTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ public function testReadRelated()
230230
/** @var Post $post */
231231
$post = factory(Post::class)->create();
232232
$tags = factory(Tag::class, 2)->create();
233-
234-
$expected = $tags->sortBy('name')->map(function (Tag $tag) {
235-
return $tag->getRouteKey();
236-
});
233+
$expected = $tags->sortBy('name');
237234

238235
$post->tags()->sync($tags);
239236

0 commit comments

Comments
 (0)
0