From 3cc18c1da22cee857f5512a60c15da6ead6dc964 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Mon, 22 Mar 2021 09:54:18 +0100 Subject: [PATCH] Eager load ListPostsController needed relations (#2717) * Eager load ListPostsController needed relations * Add comment explaining the reason for eagerloading --- src/Api/Controller/ListPostsController.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Api/Controller/ListPostsController.php b/src/Api/Controller/ListPostsController.php index df03b6dfa..f034838d7 100644 --- a/src/Api/Controller/ListPostsController.php +++ b/src/Api/Controller/ListPostsController.php @@ -93,6 +93,17 @@ class ListPostsController extends AbstractListController $results->areMoreResults() ? null : 0 ); + // Eager load discussion for use in the policies, + // eager loading does not affect the JSON response, + // the response only includes relations included in the request. + if (! in_array('discussion', $include)) { + $include[] = 'discussion'; + } + + if (in_array('user', $include)) { + $include[] = 'user.groups'; + } + return $results->getResults()->load($include); }