]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/TagRepository.php
Merge pull request #2403 from wallabag/cleanup-install-process-in-doc
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / TagRepository.php
index 0f362f79ebb0efdc37d4a9f2599e4100e24723e6..e76878d49d6e7cc5bb06c0f2a1d8227ef2bec4bc 100644 (file)
@@ -6,18 +6,65 @@ use Doctrine\ORM\EntityRepository;
 
 class TagRepository extends EntityRepository
 {
-    public function findByEntries($entryId)
+    /**
+     * 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)
     {
-        $qb = $this->createQueryBuilder('t')
-            ->select('t')
-            ->leftJoin('t.id', 'u')
-            ->where('e.isStarred = true')
-            ->andWhere('u.id =:userId')->setParameter('userId', $userId)
-            ->orderBy('e.createdAt', 'desc')
+        $query = $this->createQueryBuilder('t')
+            ->select('t.slug')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.user = :userId')->setParameter('userId', $userId)
+            ->groupBy('t.slug')
             ->getQuery();
 
-        $paginator = new Paginator($qb);
+        if (null !== $cacheLifeTime) {
+            $query->useQueryCache(true);
+            $query->useResultCache(true);
+            $query->setResultCacheLifetime($cacheLifeTime);
+        }
 
-        return $paginator;
+        return count($query->getArrayResult());
+    }
+
+    /**
+     * Find all tags per user.
+     *
+     * @param int $userId
+     *
+     * @return array
+     */
+    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)
+            ->groupBy('t.slug')
+            ->addGroupBy('t.label')
+            ->addGroupBy('t.id')
+            ->getQuery()
+            ->getArrayResult();
+    }
+
+    /**
+     * Used only in test case to get a tag for our entry.
+     *
+     * @return Tag
+     */
+    public function findOneByEntryAndTagLabel($entry, $label)
+    {
+        return $this->createQueryBuilder('t')
+            ->leftJoin('t.entries', 'e')
+            ->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
+            ->andWhere('t.label = :label')->setParameter('label', $label)
+            ->setMaxResults(1)
+            ->getQuery()
+            ->getSingleResult();
     }
 }