Skip to content
Snippets Groups Projects
Verified Commit 21b1fb6d authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[TASK] Add comments to the findRelated functionality

parent ec1b1a90
No related branches found
No related tags found
1 merge request!41[TASK] Add related news by tag or category
...@@ -518,6 +518,7 @@ class NewsRepository extends AbstractRepository { ...@@ -518,6 +518,7 @@ class NewsRepository extends AbstractRepository {
public function findRelated(News $news, int $limit = 0) { public function findRelated(News $news, int $limit = 0) {
$connection = $this->getConnection(); $connection = $this->getConnection();
$qb = $connection->createQueryBuilder(); $qb = $connection->createQueryBuilder();
// We need to build the constraint for the tags/categories
$constraints = []; $constraints = [];
$tags = $news->getTags(); $tags = $news->getTags();
if ($tags->count() > 0) { if ($tags->count() > 0) {
...@@ -533,6 +534,7 @@ class NewsRepository extends AbstractRepository { ...@@ -533,6 +534,7 @@ class NewsRepository extends AbstractRepository {
$constraints[] = $qb->expr()->eq('pid', $news->getPid()); $constraints[] = $qb->expr()->eq('pid', $news->getPid());
} }
// here we fetch the lastUpdated of the $limit amount of news with newer lastUpdated dates
$result = $qb->select('lastUpdated') $result = $qb->select('lastUpdated')
->from('pages') ->from('pages')
->where( ->where(
...@@ -541,10 +543,11 @@ class NewsRepository extends AbstractRepository { ...@@ -541,10 +543,11 @@ class NewsRepository extends AbstractRepository {
$qb->expr()->andX(...$constraints) $qb->expr()->andX(...$constraints)
) )
->setMaxResults($limit) ->setMaxResults($limit)
->orderBy('lastUpdated', 'asc') ->orderBy('lastUpdated', 'desc')
->execute(); ->execute();
$max = $result->fetchOne(); $newest = $result->fetchOne();
// Here we fetch the lastUpdated of the $limit amount of news with older lastUpdated dates
$result = $qb->select('lastUpdated') $result = $qb->select('lastUpdated')
->from('pages') ->from('pages')
->where( ->where(
...@@ -553,9 +556,9 @@ class NewsRepository extends AbstractRepository { ...@@ -553,9 +556,9 @@ class NewsRepository extends AbstractRepository {
$qb->expr()->andX(...$constraints) $qb->expr()->andX(...$constraints)
) )
->setMaxResults($limit) ->setMaxResults($limit)
->orderBy('lastUpdated', 'desc') ->orderBy('lastUpdated', 'asc')
->execute(); ->execute();
$min = $result->fetchOne(); $oldest = $result->fetchOne();
$query = $this->createQuery(); $query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(FALSE); $query->getQuerySettings()->setRespectStoragePage(FALSE);
...@@ -563,13 +566,17 @@ class NewsRepository extends AbstractRepository { ...@@ -563,13 +566,17 @@ class NewsRepository extends AbstractRepository {
'crdate' => QueryInterface::ORDER_DESCENDING 'crdate' => QueryInterface::ORDER_DESCENDING
]); ]);
$constraints = []; $constraints = [];
if ($max) { if ($newest) {
$constraints[] = $query->lessThan('lastUpdated', $max); $constraints[] = $query->lessThanOrEqual('lastUpdated', $newest);
} }
if ($min) { if ($oldest) {
$constraints[] = $query->greaterThan('lastUpdated', $min); $constraints[] = $query->greaterThanOrEqual('lastUpdated', $oldest);
} }
// Now we fetch the $limit amount of news via extbase query and limit them to the newest and oldest date
// remember, we fetched the oldest and newest date from the $limit amount of news newer and older then
// the given news. If we limit the result of the following query to $limit, we get $limit amount of news
// "around" the given news, where newer news are preferred due to the ordering.
$tags = $news->getTags(); $tags = $news->getTags();
if ($tags->count() > 0) { if ($tags->count() > 0) {
foreach ($tags as $tag) { foreach ($tags as $tag) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment