Remove ability to have custom relationship pre-loaders for now
This commit is contained in:
parent
5d69048ef8
commit
0c8de3a75a
|
|
@ -67,30 +67,6 @@ $type->hasOne('user')
|
|||
->dontLoad();
|
||||
```
|
||||
|
||||
### Custom Loading Logic
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
```php
|
||||
$api->resource('categories', new EloquentAdapter(Models\Category::class), function (Type $type) {
|
||||
$type->hasOne('latestPost')->type('posts')->includable(); // 1
|
||||
});
|
||||
|
||||
$api->resource('posts', new EloquentAdapter(Models\Post::class), function (Type $type) {
|
||||
$type->hasOne('user') // 2
|
||||
->includable()
|
||||
->load(function (array $models, array $relationships, Context $context) {
|
||||
// Since this request is to the `GET /categories` endpoint, $models
|
||||
// will be an array of Category models, and $relationships will be
|
||||
// an array containing the objects [1, 2] above.
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Polymorphic Relationships
|
||||
|
||||
Define a polymorphic relationship using the `polymorphic` method. Optionally you may provide an array of allowed resource types:
|
||||
|
|
|
|||
|
|
@ -103,12 +103,9 @@ trait IncludesData
|
|||
|
||||
$nextRelationshipPath = array_merge($relationshipPath, [$field]);
|
||||
|
||||
if ($load = $field->getLoad()) {
|
||||
if ($field->shouldLoad()) {
|
||||
$type = $field->getType();
|
||||
|
||||
if (is_callable($load)) {
|
||||
$load($models, $nextRelationshipPath, $field->hasLinkage(), $context);
|
||||
} else {
|
||||
if (is_string($type)) {
|
||||
$relatedResource = $this->api->getResource($type);
|
||||
$scope = function ($query) use ($context, $field, $relatedResource) {
|
||||
|
|
@ -135,7 +132,6 @@ trait IncludesData
|
|||
}
|
||||
|
||||
$adapter->load($models, $nextRelationshipPath, $scope, $field->hasLinkage());
|
||||
}
|
||||
|
||||
if (isset($include[$name]) && is_string($type)) {
|
||||
$relatedResource = $this->api->getResource($type);
|
||||
|
|
|
|||
|
|
@ -70,13 +70,10 @@ abstract class Relationship extends Field
|
|||
|
||||
/**
|
||||
* Allow the relationship data to be eager-loaded into the model collection.
|
||||
*
|
||||
* This is used to prevent the n+1 query problem. If null, the adapter will
|
||||
* be used to eager-load relationship data into the model collection.
|
||||
*/
|
||||
public function load(callable $callback = null)
|
||||
public function load()
|
||||
{
|
||||
$this->load = $callback ?: true;
|
||||
$this->load = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -159,7 +156,7 @@ abstract class Relationship extends Field
|
|||
/**
|
||||
* @return bool|callable
|
||||
*/
|
||||
public function getLoad()
|
||||
public function shouldLoad()
|
||||
{
|
||||
return $this->load;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,11 +40,6 @@ class RelationshipInclusionTest extends AbstractTestCase
|
|||
$this->markTestIncomplete();
|
||||
}
|
||||
|
||||
public function test_to_one_relationships_can_have_custom_preloaders()
|
||||
{
|
||||
$this->markTestIncomplete();
|
||||
}
|
||||
|
||||
public function test_to_one_relationships_can_be_not_loadable()
|
||||
{
|
||||
$this->markTestIncomplete();
|
||||
|
|
|
|||
Loading…
Reference in New Issue