Skip to content

Commit 0c8de3a

Browse files
committed
Remove ability to have custom relationship pre-loaders for now
1 parent 5d69048 commit 0c8de3a

File tree

4 files changed

+28
-64
lines changed

4 files changed

+28
-64
lines changed

docs/relationships.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,6 @@ $type->hasOne('user')
6767
->dontLoad();
6868
```
6969

70-
### Custom Loading Logic
71-
72-
Instead of using the adapter's eager-loading logic, you may wish to define your own for a relationship. You can do so using the `load` method.
73-
74-
Beware that this can be complicated as eager-loading always takes place on the set of models at the root level of the document; these are passed as the first parameter. The second parameter is an array of the `Relationship` objects that make up the nested inclusion trail leading to the current relationship.
75-
76-
So, for example, if a request was made to `GET /categories?include=latestPost.user`, then the custom loading logic for the `user` relationship might look like this:
77-
78-
```php
79-
$api->resource('categories', new EloquentAdapter(Models\Category::class), function (Type $type) {
80-
$type->hasOne('latestPost')->type('posts')->includable(); // 1
81-
});
82-
83-
$api->resource('posts', new EloquentAdapter(Models\Post::class), function (Type $type) {
84-
$type->hasOne('user') // 2
85-
->includable()
86-
->load(function (array $models, array $relationships, Context $context) {
87-
// Since this request is to the `GET /categories` endpoint, $models
88-
// will be an array of Category models, and $relationships will be
89-
// an array containing the objects [1, 2] above.
90-
});
91-
});
92-
```
93-
9470
## Polymorphic Relationships
9571

9672
Define a polymorphic relationship using the `polymorphic` method. Optionally you may provide an array of allowed resource types:

src/Endpoint/Concerns/IncludesData.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,36 @@ private function loadRelationshipsAtLevel(array $models, array $relationshipPath
103103

104104
$nextRelationshipPath = array_merge($relationshipPath, [$field]);
105105

106-
if ($load = $field->getLoad()) {
106+
if ($field->shouldLoad()) {
107107
$type = $field->getType();
108108

109-
if (is_callable($load)) {
110-
$load($models, $nextRelationshipPath, $field->hasLinkage(), $context);
109+
if (is_string($type)) {
110+
$relatedResource = $this->api->getResource($type);
111+
$scope = function ($query) use ($context, $field, $relatedResource) {
112+
run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]);
113+
run_callbacks($field->getListeners('scope'), [$query, $context]);
114+
};
111115
} else {
112-
if (is_string($type)) {
113-
$relatedResource = $this->api->getResource($type);
114-
$scope = function ($query) use ($context, $field, $relatedResource) {
115-
run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]);
116-
run_callbacks($field->getListeners('scope'), [$query, $context]);
117-
};
118-
} else {
119-
$relatedResources = is_array($type) ? array_map(function ($type) {
120-
return $this->api->getResource($type);
121-
}, $type) : $this->api->getResources();
122-
123-
$scope = array_combine(
124-
array_map(function ($relatedResource) {
125-
return $relatedResource->getType();
126-
}, $relatedResources),
127-
128-
array_map(function ($relatedResource) use ($context, $field) {
129-
return function ($query) use ($context, $field, $relatedResource) {
130-
run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]);
131-
run_callbacks($field->getListeners('scope'), [$query, $context]);
132-
};
133-
}, $relatedResources)
134-
);
135-
}
136-
137-
$adapter->load($models, $nextRelationshipPath, $scope, $field->hasLinkage());
116+
$relatedResources = is_array($type) ? array_map(function ($type) {
117+
return $this->api->getResource($type);
118+
}, $type) : $this->api->getResources();
119+
120+
$scope = array_combine(
121+
array_map(function ($relatedResource) {
122+
return $relatedResource->getType();
123+
}, $relatedResources),
124+
125+
array_map(function ($relatedResource) use ($context, $field) {
126+
return function ($query) use ($context, $field, $relatedResource) {
127+
run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]);
128+
run_callbacks($field->getListeners('scope'), [$query, $context]);
129+
};
130+
}, $relatedResources)
131+
);
138132
}
139133

134+
$adapter->load($models, $nextRelationshipPath, $scope, $field->hasLinkage());
135+
140136
if (isset($include[$name]) && is_string($type)) {
141137
$relatedResource = $this->api->getResource($type);
142138

src/Schema/Relationship.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,10 @@ public function withoutLinkage()
7070

7171
/**
7272
* Allow the relationship data to be eager-loaded into the model collection.
73-
*
74-
* This is used to prevent the n+1 query problem. If null, the adapter will
75-
* be used to eager-load relationship data into the model collection.
7673
*/
77-
public function load(callable $callback = null)
74+
public function load()
7875
{
79-
$this->load = $callback ?: true;
76+
$this->load = true;
8077

8178
return $this;
8279
}
@@ -159,7 +156,7 @@ public function hasLinkage(): bool
159156
/**
160157
* @return bool|callable
161158
*/
162-
public function getLoad()
159+
public function shouldLoad()
163160
{
164161
return $this->load;
165162
}

tests/feature/RelationshipInclusionTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ public function test_included_to_one_relationships_are_preloaded_via_the_adapter
4040
$this->markTestIncomplete();
4141
}
4242

43-
public function test_to_one_relationships_can_have_custom_preloaders()
44-
{
45-
$this->markTestIncomplete();
46-
}
47-
4843
public function test_to_one_relationships_can_be_not_loadable()
4944
{
5045
$this->markTestIncomplete();

0 commit comments

Comments
 (0)