X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FEntryRepository.php;h=5538ae82b40081101d2482271a7b0b290039bfbf;hb=e1779760999f676cb7a74a201bc2a389959702d4;hp=a33d698d847e76ea67192b8dcec78c53aa6686cf;hpb=eaf95758dc3ebc0e666a8317cd37311f03cd2233;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index a33d698d..5538ae82 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -2,109 +2,88 @@ namespace Wallabag\CoreBundle\Repository; -use Doctrine\ORM\Query; 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) - ->andWhere('e.isDeleted=false') - ->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) - ->andWhere('e.isDeleted=false') - ->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) - ->andWhere('e.isDeleted = false') - ->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 - * @param bool $isStarred - * @param bool $isDeleted - * @param string $sort - * @param string $order + * @param int $userId + * @param bool $isArchived + * @param bool $isStarred + * @param string $sort + * @param string $order * - * @return ArrayCollection + * @return array */ - public function findEntries($userId, $isArchived = null, $isStarred = null, $isDeleted = null, $sort = 'created', $order = 'ASC') + public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC') { $qb = $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('u.id =:userId')->setParameter('userId', $userId); + ->where('e.user =:userId')->setParameter('userId', $userId); if (null !== $isArchived) { $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); @@ -114,18 +93,31 @@ class EntryRepository extends EntityRepository $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); } - if (null !== $isDeleted) { - $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', (bool) $isDeleted); - } - 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); + } + + /** + * Fetch an entry with a tag. Only used for tests. + * + * @return Entry + */ + public function findOneWithTags($userId) + { + $qb = $this->createQueryBuilder('e') + ->innerJoin('e.tags', 't') + ->innerJoin('e.user', 'u') + ->addSelect('t', 'u') + ->where('e.user=:userId')->setParameter('userId', $userId) + ; + + return $qb->getQuery()->getResult(); } }