diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index e4607141..6dde1fdc 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -18,6 +18,7 @@ class CommentController $postCount = PostSearchService::getEntityCount($searchQuery); $pageCount = ceil($postCount / $commentsPerPage); PostModel::preloadTags($posts); + PostModel::preloadComments($posts); $comments = []; foreach ($posts as $post) $comments = array_merge($comments, $post->getComments()); diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 399373dd..2df06196 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -174,6 +174,47 @@ class PostModel extends AbstractCrudModel + public static function preloadComments($posts) + { + if (empty($posts)) + return; + + $postMap = []; + $tagsMap = []; + foreach ($posts as $post) + { + $postId = $post->id; + $postMap[$postId] = $post; + $commentMap[$postId] = []; + } + $postIds = array_unique(array_keys($postMap)); + + $stmt = new SqlSelectStatement(); + $stmt->setTable('comment'); + $stmt->addColumn('comment.*'); + $stmt->addColumn('post_id'); + $stmt->setCriterion(SqlInOperator::fromArray('post_id', SqlBinding::fromArray($postIds))); + $rows = Database::fetchAll($stmt); + + foreach ($rows as $row) + { + if (isset($comments[$row['id']])) + continue; + unset($row['post_id']); + $comment = CommentModel::convertRow($row); + $comments[$row['id']] = $comment; + } + + foreach ($rows as $row) + { + $postId = $row['post_id']; + $commentMap[$postId] []= $comments[$row['id']]; + } + + foreach ($commentMap as $postId => $comments) + $postMap[$postId]->setCache('comments', $comments); + } + public static function preloadTags($posts) { if (empty($posts))