mirror of
https://github.com/flarum/core.git
synced 2025-08-18 06:11:23 +02:00
Eager loading extender (#2724)
* Eager loading extender * Add tests for the eager loading extender
This commit is contained in:
@@ -29,6 +29,7 @@ class ApiController implements ExtenderInterface
|
||||
private $addSortFields = [];
|
||||
private $removeSortFields = [];
|
||||
private $sort;
|
||||
private $load = [];
|
||||
|
||||
/**
|
||||
* @param string $controllerClass The ::class attribute of the controller you are modifying.
|
||||
@@ -216,6 +217,27 @@ class ApiController implements ExtenderInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Eager loads relationships needed for serializer logic.
|
||||
*
|
||||
* First level relationships will be loaded regardless of whether they are included in the response.
|
||||
* Sublevel relationships will only be loaded if the upper level was included or manually loaded.
|
||||
*
|
||||
* @example If a relationship such as: 'relation.subRelation' is specified,
|
||||
* it will only be loaded if 'relation' is or has been loaded.
|
||||
* To force load the relationship, both levels have to be specified,
|
||||
* example: ['relation', 'relation.subRelation'].
|
||||
*
|
||||
* @param string|array
|
||||
* @return self
|
||||
*/
|
||||
public function load($relations)
|
||||
{
|
||||
$this->load = array_merge($this->load, (array) $relations);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container, Extension $extension = null)
|
||||
{
|
||||
$this->beforeDataCallbacks[] = function (AbstractSerializeController $controller) use ($container) {
|
||||
@@ -281,6 +303,8 @@ class ApiController implements ExtenderInterface
|
||||
$beforeSerializationCallback = ContainerUtil::wrapCallback($beforeSerializationCallback, $container);
|
||||
AbstractSerializeController::addSerializationPreparationCallback($this->controllerClass, $beforeSerializationCallback);
|
||||
}
|
||||
|
||||
AbstractSerializeController::setLoadRelations($this->controllerClass, $this->load);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user