From f25c9acf59ef90c5b9788ccd7731fed3727dc346 Mon Sep 17 00:00:00 2001
From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
Date: Tue, 26 Apr 2022 11:24:47 +0200
Subject: [PATCH] [BUGFIX] Fix bugs with fetching the related news

---
 Classes/Domain/Repository/NewsRepository.php | 27 ++++++++++++++------
 Classes/ViewHelpers/RelatedViewHelper.php    |  2 +-
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/Classes/Domain/Repository/NewsRepository.php b/Classes/Domain/Repository/NewsRepository.php
index 7d8011d..6a0b198 100644
--- a/Classes/Domain/Repository/NewsRepository.php
+++ b/Classes/Domain/Repository/NewsRepository.php
@@ -536,29 +536,40 @@ class NewsRepository extends AbstractRepository {
 			$constraints[] = $qb->expr()->eq('pid', $news->getPid());
 		}
 
+		if ($limit > 0) {
+			$qb->setMaxResults($limit);
+		}
+
 		// here we fetch the lastUpdated of the $limit amount of news with newer lastUpdated dates
 		$result = $qb->select('lastUpdated')
-			->from('pages')
+			->from('pages', 'pages')
 			->where(
 				$qb->expr()->eq('doktype', $qb->createNamedParameter(News::DOK_TYPE_NEWS, Connection::PARAM_INT)),
-				$qb->expr()->gt('lastUpdated', $news->getLastUpdated()->getTimestamp()),
-				$qb->expr()->andX(...$constraints)
-			)
-			->setMaxResults($limit)
+				$qb->expr()->gt('lastUpdated', $news->getLastUpdated()->getTimestamp())
+			)->andWhere(...$constraints)
 			->orderBy('lastUpdated', 'desc')
 			->execute();
 		$newest = $result->fetchOne();
 
 		// Here we fetch the lastUpdated of the $limit amount of news with older lastUpdated dates
-		$result = $qb->orderBy('lastUpdated', 'asc')->execute();
+		$result = $qb->where(
+				$qb->expr()->eq('doktype', $qb->createNamedParameter(News::DOK_TYPE_NEWS, Connection::PARAM_INT)),
+				$qb->expr()->lt('lastUpdated', $news->getLastUpdated()->getTimestamp())
+			)->andWhere(...$constraints)
+			->orderBy('lastUpdated', 'asc')
+			->execute();
 		$oldest = $result->fetchOne();
 
 		$query = $this->createQuery();
 		$query->getQuerySettings()->setRespectStoragePage(FALSE);
 		$query->setOrderings([
-			'crdate' => QueryInterface::ORDER_DESCENDING
+			'lastUpdated' => QueryInterface::ORDER_DESCENDING
 		]);
-		$constraints = [];
+		$constraints = [
+			$query->logicalNot(
+				$query->equals('uid', $news->getUid())
+			)
+		];
 		if ($newest) {
 			$constraints[] = $query->lessThanOrEqual('lastUpdated', $newest);
 		}
diff --git a/Classes/ViewHelpers/RelatedViewHelper.php b/Classes/ViewHelpers/RelatedViewHelper.php
index 6a5ec43..b523ff8 100644
--- a/Classes/ViewHelpers/RelatedViewHelper.php
+++ b/Classes/ViewHelpers/RelatedViewHelper.php
@@ -47,7 +47,7 @@ class RelatedViewHelper extends AbstractViewHelper {
 		$templateVariableContainer = $renderingContext->getVariableProvider();
 		$news = $arguments['news'];
 		$newsRepository = GeneralUtility::makeInstance(NewsRepository::class);
-		$related = $newsRepository->findRelated($news);
+		$related = $newsRepository->findRelated($news, (int) $arguments['limit']);
 		if (isset($arguments['iteration'])) {
 			$iterationData = [
 				'index' => 0,
-- 
GitLab