mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 09:41:49 +08:00
perf: Allow loading relations in other discussion endpoints (#3191)
This commit is contained in:
parent
dc48e2327b
commit
c7791b63f7
|
@ -157,7 +157,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||
*
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRelationsToLoad(): array
|
||||
protected function getRelationsToLoad(Collection $models): array
|
||||
{
|
||||
$addedRelations = [];
|
||||
|
||||
|
@ -175,7 +175,7 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||
*
|
||||
* @return array<string, callable>
|
||||
*/
|
||||
protected function getRelationCallablesToLoad(): array
|
||||
protected function getRelationCallablesToLoad(Collection $models): array
|
||||
{
|
||||
$addedRelationCallables = [];
|
||||
|
||||
|
@ -193,8 +193,8 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
|
|||
*/
|
||||
protected function loadRelations(Collection $models, array $relations, ServerRequestInterface $request = null): void
|
||||
{
|
||||
$addedRelations = $this->getRelationsToLoad();
|
||||
$addedRelationCallables = $this->getRelationCallablesToLoad();
|
||||
$addedRelations = $this->getRelationsToLoad($models);
|
||||
$addedRelationCallables = $this->getRelationCallablesToLoad($models);
|
||||
|
||||
foreach ($addedRelationCallables as $name => $relation) {
|
||||
$addedRelations[] = $name;
|
||||
|
|
|
@ -14,6 +14,7 @@ use Flarum\Discussion\Command\ReadDiscussion;
|
|||
use Flarum\Discussion\Command\StartDiscussion;
|
||||
use Flarum\Http\RequestUtil;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Arr;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
@ -70,6 +71,8 @@ class CreateDiscussionController extends AbstractCreateController
|
|||
);
|
||||
}
|
||||
|
||||
$this->loadRelations(new Collection([$discussion]), $this->extractInclude($request), $request);
|
||||
|
||||
return $discussion;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ use Flarum\Http\RequestUtil;
|
|||
use Flarum\Http\SlugManager;
|
||||
use Flarum\Post\PostRepository;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
@ -98,9 +99,9 @@ class ShowDiscussionController extends AbstractShowController
|
|||
$this->includePosts($discussion, $request, $postRelationships);
|
||||
}
|
||||
|
||||
$discussion->load(array_filter($include, function ($relationship) {
|
||||
$this->loadRelations(new Collection([$discussion]), array_filter($include, function ($relationship) {
|
||||
return ! Str::startsWith($relationship, 'posts');
|
||||
}));
|
||||
}), $request);
|
||||
|
||||
return $discussion;
|
||||
}
|
||||
|
@ -198,10 +199,29 @@ class ShowDiscussionController extends AbstractShowController
|
|||
return $posts->all();
|
||||
}
|
||||
|
||||
protected function getRelationsToLoad(): array
|
||||
protected function getRelationsToLoad(Collection $models): array
|
||||
{
|
||||
$addedRelations = parent::getRelationsToLoad();
|
||||
$addedRelations = parent::getRelationsToLoad($models);
|
||||
|
||||
if ($models->first() instanceof Discussion) {
|
||||
return $addedRelations;
|
||||
}
|
||||
|
||||
return $this->getPostRelationships($addedRelations);
|
||||
}
|
||||
|
||||
protected function getRelationCallablesToLoad(Collection $models): array
|
||||
{
|
||||
$addedCallableRelations = parent::getRelationCallablesToLoad($models);
|
||||
|
||||
if ($models->first() instanceof Discussion) {
|
||||
return $addedCallableRelations;
|
||||
}
|
||||
|
||||
$postCallableRelationships = $this->getPostRelationships(array_keys($addedCallableRelations));
|
||||
|
||||
return array_intersect_key($addedCallableRelations, array_flip(array_map(function ($relation) {
|
||||
return "posts.$relation";
|
||||
}, $postCallableRelationships)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user