mirror of
https://github.com/flarum/core.git
synced 2025-05-17 20:59:44 +02:00
perf: Allow eager loading posts relations of GET discussion endpoint (#3048)
This commit is contained in:
parent
6defca5a6d
commit
d333d0b0e6
@ -148,13 +148,9 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||||||
abstract protected function createElement($data, SerializerInterface $serializer);
|
abstract protected function createElement($data, SerializerInterface $serializer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Eager loads the required relationships.
|
* Returns the relations to load added by extenders.
|
||||||
*
|
|
||||||
* @param Collection $models
|
|
||||||
* @param array $relations
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected function loadRelations(Collection $models, array $relations): void
|
protected function getRelationsToLoad(): array
|
||||||
{
|
{
|
||||||
$addedRelations = [];
|
$addedRelations = [];
|
||||||
|
|
||||||
@ -164,6 +160,20 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $addedRelations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Eager loads the required relationships.
|
||||||
|
*
|
||||||
|
* @param Collection $models
|
||||||
|
* @param array $relations
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function loadRelations(Collection $models, array $relations): void
|
||||||
|
{
|
||||||
|
$addedRelations = $this->getRelationsToLoad();
|
||||||
|
|
||||||
if (! empty($addedRelations)) {
|
if (! empty($addedRelations)) {
|
||||||
usort($addedRelations, function ($a, $b) {
|
usort($addedRelations, function ($a, $b) {
|
||||||
return substr_count($a, '.') - substr_count($b, '.');
|
return substr_count($a, '.') - substr_count($b, '.');
|
||||||
|
@ -187,12 +187,21 @@ class ShowDiscussionController extends AbstractShowController
|
|||||||
|
|
||||||
$query->orderBy('created_at')->skip($offset)->take($limit)->with($include);
|
$query->orderBy('created_at')->skip($offset)->take($limit)->with($include);
|
||||||
|
|
||||||
$posts = $query->get()->all();
|
$posts = $query->get();
|
||||||
|
|
||||||
foreach ($posts as $post) {
|
foreach ($posts as $post) {
|
||||||
$post->discussion = $discussion;
|
$post->discussion = $discussion;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $posts;
|
$this->loadRelations($posts, $include);
|
||||||
|
|
||||||
|
return $posts->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRelationsToLoad(): array
|
||||||
|
{
|
||||||
|
$addedRelations = parent::getRelationsToLoad();
|
||||||
|
|
||||||
|
return $this->getPostRelationships($addedRelations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user