]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/EntryRepository.php
CS
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
index f4c803f991c2e38fb41d510aa5f44ff3e197e1ae..1335e808666bc05eb63042d84b080482d7dff368 100644 (file)
@@ -2,30 +2,31 @@
 
 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
+     * Retrieves unread entries for a user.
+     *
+     * @param int $userId
+     * @param int $firstResult
+     * @param int $maxResults
      *
-     * @param $userId
-     * @param $firstResult
-     * @param  int       $maxResults
      * @return Paginator
      */
     public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
     {
         $qb = $this->createQueryBuilder('e')
-            ->select('e')
             ->setFirstResult($firstResult)
             ->setMaxResults($maxResults)
+            ->leftJoin('e.user', 'u')
             ->where('e.isArchived = false')
-            ->andWhere('e.userId =:userId')->setParameter('userId', $userId)
-            ->andWhere('e.isDeleted=false')
-            ->orderBy('e.createdAt', 'desc')
+            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
+            ->orderBy('e.id', 'desc')
             ->getQuery();
 
         $paginator = new Paginator($qb);
@@ -34,11 +35,12 @@ class EntryRepository extends EntityRepository
     }
 
     /**
-     * Retrieves read entries for a user
+     * Retrieves read entries for a user.
+     *
+     * @param int $userId
+     * @param int $firstResult
+     * @param int $maxResults
      *
-     * @param $userId
-     * @param $firstResult
-     * @param  int       $maxResults
      * @return Paginator
      */
     public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
@@ -47,10 +49,10 @@ class EntryRepository extends EntityRepository
             ->select('e')
             ->setFirstResult($firstResult)
             ->setMaxResults($maxResults)
+            ->leftJoin('e.user', 'u')
             ->where('e.isArchived = true')
-            ->andWhere('e.userId =:userId')->setParameter('userId', $userId)
-            ->andWhere('e.isDeleted=false')
-            ->orderBy('e.createdAt', 'desc')
+            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
+            ->orderBy('e.id', 'desc')
             ->getQuery();
 
         $paginator = new Paginator($qb);
@@ -59,11 +61,12 @@ class EntryRepository extends EntityRepository
     }
 
     /**
-     * Retrieves starred entries for a user
+     * Retrieves starred entries for a user.
+     *
+     * @param int $userId
+     * @param int $firstResult
+     * @param int $maxResults
      *
-     * @param $userId
-     * @param $firstResult
-     * @param  int       $maxResults
      * @return Paginator
      */
     public function findStarredByUser($userId, $firstResult, $maxResults = 12)
@@ -72,10 +75,10 @@ class EntryRepository extends EntityRepository
             ->select('e')
             ->setFirstResult($firstResult)
             ->setMaxResults($maxResults)
+            ->leftJoin('e.user', 'u')
             ->where('e.isStarred = true')
-            ->andWhere('e.userId =:userId')->setParameter('userId', $userId)
-            ->andWhere('e.isDeleted=false')
-            ->orderBy('e.createdAt', 'desc')
+            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
+            ->orderBy('e.id', 'desc')
             ->getQuery();
 
         $paginator = new Paginator($qb);
@@ -83,32 +86,55 @@ class EntryRepository extends EntityRepository
         return $paginator;
     }
 
-    public function findEntries($userId, $isArchived, $isStarred, $isDeleted, $sort, $order)
+    /**
+     * Find Entries.
+     *
+     * @param int    $userId
+     * @param bool   $isArchived
+     * @param bool   $isStarred
+     * @param string $sort
+     * @param string $order
+     *
+     * @return array
+     */
+    public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
     {
         $qb = $this->createQueryBuilder('e')
-            ->select('e')
-            ->where('e.userId =:userId')->setParameter('userId', $userId);
-
-        if (!is_null($isArchived)) {
-            $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', $isArchived);
-        }
+            ->where('e.user =:userId')->setParameter('userId', $userId);
 
-        if (!is_null($isStarred)) {
-            $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', $isStarred);
+        if (null !== $isArchived) {
+            $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
         }
 
-        if (!is_null($isDeleted)) {
-            $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', $isDeleted);
+        if (null !== $isStarred) {
+            $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
         }
 
         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(Query::HYDRATE_ARRAY);
+        $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();
     }
 }