diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2018-10-12 21:41:28 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2018-10-12 22:14:12 +0200 |
commit | a06ebf826714feb7c48859ff2877b1b8904b6e07 (patch) | |
tree | 9d276b12ee4e27af9f6497488dd4f54c027f810d | |
parent | ba335762a80e7860a166c9191502e9c35a5d76b4 (diff) | |
download | wallabag-a06ebf826714feb7c48859ff2877b1b8904b6e07.tar.gz wallabag-a06ebf826714feb7c48859ff2877b1b8904b6e07.tar.zst wallabag-a06ebf826714feb7c48859ff2877b1b8904b6e07.zip |
Better random function
-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 71ed7b6d..6941eaee 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -436,17 +436,14 @@ class EntryRepository extends EntityRepository | |||
436 | * @throws \Doctrine\ORM\NoResultException | 436 | * @throws \Doctrine\ORM\NoResultException |
437 | * @throws \Doctrine\ORM\NonUniqueResultException | 437 | * @throws \Doctrine\ORM\NonUniqueResultException |
438 | * | 438 | * |
439 | * @see https://github.com/doctrine/doctrine2/issues/5479#issuecomment-403862934 | ||
440 | * | ||
439 | * @return Entry | 441 | * @return Entry |
440 | */ | 442 | */ |
441 | public function getRandomEntry($userId, $status = '') | 443 | public function getRandomEntry($userId, $status = '') |
442 | { | 444 | { |
443 | $max = $this->getEntityManager() | 445 | $qb = $this->getQueryBuilderByUser($userId) |
444 | ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') | 446 | ->select('MIN(e.id)', 'MAX(e.id)'); |
445 | ->setParameter('userId', $userId) | ||
446 | ->getSingleScalarResult(); | ||
447 | |||
448 | $qb = $this->createQueryBuilder('e') | ||
449 | ->where('e.user = :user_id')->setParameter('user_id', $userId); | ||
450 | 447 | ||
451 | if ('unread' === $status) { | 448 | if ('unread' === $status) { |
452 | $qb->andWhere('e.isArchived = false'); | 449 | $qb->andWhere('e.isArchived = false'); |
@@ -465,8 +462,15 @@ class EntryRepository extends EntityRepository | |||
465 | $qb->andWhere('t.id is null'); | 462 | $qb->andWhere('t.id is null'); |
466 | } | 463 | } |
467 | 464 | ||
468 | return $qb->andWhere('e.id >= :rand') | 465 | $idLimits = $qb |
469 | ->setParameter('rand', rand(0, $max)) | 466 | ->getQuery() |
467 | ->getOneOrNullResult(); | ||
468 | $randomPossibleIds = rand($idLimits[1], $idLimits[2]); | ||
469 | |||
470 | return $qb | ||
471 | ->select('e') | ||
472 | ->andWhere('e.id >= :random_id') | ||
473 | ->setParameter('random_id', $randomPossibleIds) | ||
470 | ->setMaxResults(1) | 474 | ->setMaxResults(1) |
471 | ->getQuery() | 475 | ->getQuery() |
472 | ->getSingleResult(); | 476 | ->getSingleResult(); |