diff options
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 6107e19e..05563079 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -440,17 +440,14 @@ class EntryRepository extends EntityRepository | |||
440 | * @throws \Doctrine\ORM\NoResultException | 440 | * @throws \Doctrine\ORM\NoResultException |
441 | * @throws \Doctrine\ORM\NonUniqueResultException | 441 | * @throws \Doctrine\ORM\NonUniqueResultException |
442 | * | 442 | * |
443 | * @see https://github.com/doctrine/doctrine2/issues/5479#issuecomment-403862934 | ||
444 | * | ||
443 | * @return Entry | 445 | * @return Entry |
444 | */ | 446 | */ |
445 | public function getRandomEntry($userId, $status = '') | 447 | public function getRandomEntry($userId, $status = '') |
446 | { | 448 | { |
447 | $max = $this->getEntityManager() | 449 | $qb = $this->getQueryBuilderByUser($userId) |
448 | ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') | 450 | ->select('MIN(e.id)', 'MAX(e.id)'); |
449 | ->setParameter('userId', $userId) | ||
450 | ->getSingleScalarResult(); | ||
451 | |||
452 | $qb = $this->createQueryBuilder('e') | ||
453 | ->where('e.user = :user_id')->setParameter('user_id', $userId); | ||
454 | 451 | ||
455 | if ('unread' === $status) { | 452 | if ('unread' === $status) { |
456 | $qb->andWhere('e.isArchived = false'); | 453 | $qb->andWhere('e.isArchived = false'); |
@@ -469,8 +466,15 @@ class EntryRepository extends EntityRepository | |||
469 | $qb->andWhere('t.id is null'); | 466 | $qb->andWhere('t.id is null'); |
470 | } | 467 | } |
471 | 468 | ||
472 | return $qb->andWhere('e.id >= :rand') | 469 | $idLimits = $qb |
473 | ->setParameter('rand', rand(0, $max)) | 470 | ->getQuery() |
471 | ->getOneOrNullResult(); | ||
472 | $randomPossibleIds = rand($idLimits[1], $idLimits[2]); | ||
473 | |||
474 | return $qb | ||
475 | ->select('e') | ||
476 | ->andWhere('e.id >= :random_id') | ||
477 | ->setParameter('random_id', $randomPossibleIds) | ||
474 | ->setMaxResults(1) | 478 | ->setMaxResults(1) |
475 | ->getQuery() | 479 | ->getQuery() |
476 | ->getSingleResult(); | 480 | ->getSingleResult(); |