aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository/EntryRepository.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php33
1 files changed, 12 insertions, 21 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 879e6671..fabb1963 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -325,8 +325,8 @@ class EntryRepository extends EntityRepository
325 * Find an entry by its url and its owner. 325 * Find an entry by its url and its owner.
326 * If it exists, return the entry otherwise return false. 326 * If it exists, return the entry otherwise return false.
327 * 327 *
328 * @param $url 328 * @param string $url
329 * @param $userId 329 * @param int $userId
330 * 330 *
331 * @return Entry|bool 331 * @return Entry|bool
332 */ 332 */
@@ -417,8 +417,8 @@ class EntryRepository extends EntityRepository
417 /** 417 /**
418 * Find all entries by url and owner. 418 * Find all entries by url and owner.
419 * 419 *
420 * @param $url 420 * @param string $url
421 * @param $userId 421 * @param int $userId
422 * 422 *
423 * @return array 423 * @return array
424 */ 424 */
@@ -434,20 +434,17 @@ class EntryRepository extends EntityRepository
434 /** 434 /**
435 * Returns a random entry, filtering by status. 435 * Returns a random entry, filtering by status.
436 * 436 *
437 * @param $userId 437 * @param int $userId
438 * @param string $type can be unread, archive, starred, etc 438 * @param string $type Can be unread, archive, starred, etc
439 * 439 *
440 * @throws \Doctrine\ORM\NoResultException 440 * @throws \Doctrine\ORM\NoResultException
441 * @throws \Doctrine\ORM\NonUniqueResultException
442 *
443 * @see https://github.com/doctrine/doctrine2/issues/5479#issuecomment-403862934
444 * 441 *
445 * @return Entry 442 * @return Entry
446 */ 443 */
447 public function getRandomEntry($userId, $type = '') 444 public function getRandomEntry($userId, $type = '')
448 { 445 {
449 $qb = $this->getQueryBuilderByUser($userId) 446 $qb = $this->getQueryBuilderByUser($userId)
450 ->select('MIN(e.id)', 'MAX(e.id)'); 447 ->select('e.id');
451 448
452 switch ($type) { 449 switch ($type) {
453 case 'unread': 450 case 'unread':
@@ -465,18 +462,12 @@ class EntryRepository extends EntityRepository
465 break; 462 break;
466 } 463 }
467 464
468 $idLimits = $qb 465 $ids = $qb->getQuery()->getArrayResult();
469 ->getQuery()
470 ->getOneOrNullResult();
471 $randomPossibleIds = rand($idLimits[1], $idLimits[2]);
472 466
473 return $qb 467 // random select one in the list
474 ->select('e') 468 $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id'];
475 ->andWhere('e.id >= :random_id') 469
476 ->setParameter('random_id', $randomPossibleIds) 470 return $this->find($randomId);
477 ->setMaxResults(1)
478 ->getQuery()
479 ->getSingleResult();
480 } 471 }
481 472
482 /** 473 /**