aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2018-10-12 21:41:28 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2019-01-19 21:09:33 +0100
commit062fad434ad883ffc6f0c3798f2a490688199f3c (patch)
tree43bd596d50d43110eb5130d5830d8f5be6f99d31 /src
parent0447a75b06142afe59a179bb59ee94f1978aa7a9 (diff)
downloadwallabag-062fad434ad883ffc6f0c3798f2a490688199f3c.tar.gz
wallabag-062fad434ad883ffc6f0c3798f2a490688199f3c.tar.zst
wallabag-062fad434ad883ffc6f0c3798f2a490688199f3c.zip
Better random function
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php22
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();