]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/TagRepository.php
Merge pull request #3011 from wallabag/2.3
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
index 9d127da7143b588e4b600088502ffd61c7dd654d..5c45211f6beb1e2820e9ed5ddba79f95c10a04df 100644 (file)
@@ -3,6 +3,7 @@
 namespace Wallabag\CoreBundle\Repository;
 
 use Doctrine\ORM\EntityRepository;
+use Wallabag\CoreBundle\Entity\Tag;
 
 class TagRepository extends EntityRepository
 {
@@ -33,19 +34,53 @@ class TagRepository extends EntityRepository
     }
 
     /**
-     * Find all tags with associated entries per user.
+     * Find all tags per user.
+     * Instead of just left joined on the Entry table, we select only id and group by id to avoid tag multiplication in results.
+     * Once we have all tags id, we can safely request them one by one.
+     * This'll still be fastest than the previous query.
      *
      * @param int $userId
      *
      * @return array
      */
-    public function findAllTagsWithEntries($userId)
+    public function findAllTags($userId)
     {
-        return $this->createQueryBuilder('t')
+        $ids = $this->createQueryBuilder('t')
+            ->select('t.id')
             ->leftJoin('t.entries', 'e')
             ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.id')
+            ->orderBy('t.slug')
             ->getQuery()
-            ->getResult();
+            ->getArrayResult();
+
+        $tags = [];
+        foreach ($ids as $id) {
+            $tags[] = $this->find($id);
+        }
+
+        return $tags;
+    }
+
+    /**
+     * Find all tags (flat) per user with nb entries.
+     *
+     * @param int $userId
+     *
+     * @return array
+     */
+    public function findAllFlatTagsWithNbEntries($userId)
+    {
+        return $this->createQueryBuilder('t')
+            ->select('t.id, t.label, t.slug, count(e.id) as nbEntries')
+            ->distinct(true)
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')
+            ->groupBy('t.id')
+            ->orderBy('t.slug')
+            ->setParameter('userId', $userId)
+            ->getQuery()
+            ->getArrayResult();
     }
 
     /**
@@ -63,4 +98,24 @@ class TagRepository extends EntityRepository
             ->getQuery()
             ->getSingleResult();
     }
+
+    public function findForArchivedArticlesByUser($userId)
+    {
+        $ids = $this->createQueryBuilder('t')
+            ->select('t.id')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->andWhere('e.isArchived = true')
+            ->groupBy('t.id')
+            ->orderBy('t.slug')
+            ->getQuery()
+            ->getArrayResult();
+
+        $tags = [];
+        foreach ($ids as $id) {
+            $tags[] = $this->find($id);
+        }
+
+        return $tags;
+    }
 }