]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/TagRepository.php
Merge remote-tracking branch 'origin/master' into 2.2
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
index 41f616079dd53f69032512adf6a1bfb176b98657..e76878d49d6e7cc5bb06c0f2a1d8227ef2bec4bc 100644 (file)
@@ -7,7 +7,33 @@ use Doctrine\ORM\EntityRepository;
 class TagRepository extends EntityRepository
 {
     /**
-     * Find Tags.
+     * Count all tags per user.
+     *
+     * @param int $userId
+     * @param int $cacheLifeTime Duration of the cache for this query
+     *
+     * @return int
+     */
+    public function countAllTags($userId, $cacheLifeTime = null)
+    {
+        $query = $this->createQueryBuilder('t')
+            ->select('t.slug')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.slug')
+            ->getQuery();
+
+        if (null !== $cacheLifeTime) {
+            $query->useQueryCache(true);
+            $query->useResultCache(true);
+            $query->setResultCacheLifetime($cacheLifeTime);
+        }
+
+        return count($query->getArrayResult());
+    }
+
+    /**
+     * Find all tags per user.
      *
      * @param int $userId
      *
@@ -16,8 +42,14 @@ class TagRepository extends EntityRepository
     public function findAllTags($userId)
     {
         return $this->createQueryBuilder('t')
+            ->select('t.slug', 't.label', 't.id')
             ->leftJoin('t.entries', 'e')
-            ->where('e.user = :userId')->setParameter('userId', $userId);
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.slug')
+            ->addGroupBy('t.label')
+            ->addGroupBy('t.id')
+            ->getQuery()
+            ->getArrayResult();
     }
 
     /**