X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FEntryRepository.php;h=5538ae82b40081101d2482271a7b0b290039bfbf;hb=e1779760999f676cb7a74a201bc2a389959702d4;hp=10fb9bf7933ca1f986bcce8712d640f5c2a25ef4;hpb=a36737f4859e3acbddf5cfe90c279ba725a6d88a;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 10fb9bf7..5538ae82 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -3,89 +3,74 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\Tools\Pagination\Paginator; +use Pagerfanta\Adapter\DoctrineORMAdapter; +use Pagerfanta\Pagerfanta; class EntryRepository extends EntityRepository { /** - * Retrieves unread entries for a user + * Return a query builder to used by other getBuilderFor* method. * * @param int $userId - * @param int $firstResult - * @param int $maxResults * - * @return Paginator + * @return QueryBuilder */ - public function findUnreadByUser($userId, $firstResult, $maxResults = 12) + private function getBuilderByUser($userId) { - $qb = $this->createQueryBuilder('e') - ->setFirstResult($firstResult) - ->setMaxResults($maxResults) + return $this->createQueryBuilder('e') ->leftJoin('e.user', 'u') - ->where('e.isArchived = false') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.createdAt', 'desc') - ->getQuery(); - - $paginator = new Paginator($qb); - - return $paginator; + ->andWhere('u.id = :userId')->setParameter('userId', $userId) + ->orderBy('e.id', 'desc') + ; } /** - * Retrieves read entries for a user + * Retrieves unread entries for a user. * * @param int $userId - * @param int $firstResult - * @param int $maxResults * - * @return Paginator + * @return QueryBuilder */ - public function findArchiveByUser($userId, $firstResult, $maxResults = 12) + public function getBuilderForUnreadByUser($userId) { - $qb = $this->createQueryBuilder('e') - ->select('e') - ->setFirstResult($firstResult) - ->setMaxResults($maxResults) - ->leftJoin('e.user', 'u') - ->where('e.isArchived = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.createdAt', 'desc') - ->getQuery(); - - $paginator = new Paginator($qb); - - return $paginator; + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = false') + ; } /** - * Retrieves starred entries for a user + * Retrieves read entries for a user. * * @param int $userId - * @param int $firstResult - * @param int $maxResults * - * @return Paginator + * @return QueryBuilder */ - public function findStarredByUser($userId, $firstResult, $maxResults = 12) + public function getBuilderForArchiveByUser($userId) { - $qb = $this->createQueryBuilder('e') - ->select('e') - ->setFirstResult($firstResult) - ->setMaxResults($maxResults) - ->leftJoin('e.user', 'u') - ->where('e.isStarred = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.createdAt', 'desc') - ->getQuery(); - - $paginator = new Paginator($qb); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = true') + ; + } - return $paginator; + /** + * Retrieves starred entries for a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + public function getBuilderForStarredByUser($userId) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isStarred = true') + ; } /** - * Find Entries + * Find Entries. * * @param int $userId * @param bool $isArchived @@ -109,14 +94,14 @@ class EntryRepository extends EntityRepository } if ('created' === $sort) { - $qb->orderBy('e.createdAt', $order); + $qb->orderBy('e.id', $order); } elseif ('updated' === $sort) { $qb->orderBy('e.updatedAt', $order); } - return $qb - ->getQuery() - ->getResult(); + $pagerAdapter = new DoctrineORMAdapter($qb); + + return new Pagerfanta($pagerAdapter); } /** @@ -124,14 +109,15 @@ class EntryRepository extends EntityRepository * * @return Entry */ - public function findOneWithTags() + public function findOneWithTags($userId) { $qb = $this->createQueryBuilder('e') ->innerJoin('e.tags', 't') - ->addSelect('t'); + ->innerJoin('e.user', 'u') + ->addSelect('t', 'u') + ->where('e.user=:userId')->setParameter('userId', $userId) + ; - return $qb - ->getQuery() - ->getOneOrNullResult(); + return $qb->getQuery()->getResult(); } }