From 99686ef8bba4bd230a65027e88360da52bb3cc0b Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 11 Nov 2017 22:44:45 +1030 Subject: [PATCH] Performance: Assign parent discussion to posts so it doesn't have to be reloaded --- .../Api/Controller/ListDiscussionsController.php | 14 +++++++++++++- .../Api/Controller/ShowDiscussionController.php | 8 +++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/framework/core/src/Api/Controller/ListDiscussionsController.php b/framework/core/src/Api/Controller/ListDiscussionsController.php index ab8b21283..04db95c3a 100644 --- a/framework/core/src/Api/Controller/ListDiscussionsController.php +++ b/framework/core/src/Api/Controller/ListDiscussionsController.php @@ -93,6 +93,18 @@ class ListDiscussionsController extends AbstractCollectionController $results->areMoreResults() ? null : 0 ); - return $results->getResults(); + $results = $results->getResults(); + + if ($relations = array_intersect($load, ['startPost', 'lastPost'])) { + foreach ($results as $discussion) { + foreach ($relations as $relation) { + if ($discussion->$relation) { + $discussion->$relation->discussion = $discussion; + } + } + } + } + + return $results; } } diff --git a/framework/core/src/Api/Controller/ShowDiscussionController.php b/framework/core/src/Api/Controller/ShowDiscussionController.php index 7f04ca167..e99ec54b0 100644 --- a/framework/core/src/Api/Controller/ShowDiscussionController.php +++ b/framework/core/src/Api/Controller/ShowDiscussionController.php @@ -173,6 +173,12 @@ class ShowDiscussionController extends AbstractResourceController $query->orderBy('time')->skip($offset)->take($limit)->with($include); - return $query->get()->all(); + $posts = $query->get()->all(); + + foreach ($posts as $post) { + $post->discussion = $discussion; + } + + return $posts; } }