namespace Wallabag\CoreBundle\Repository;
use Doctrine\ORM\EntityRepository;
-use Doctrine\ORM\Tools\Pagination\Paginator;
-class EntryRepository extends EntityRepository
+class TagRepository extends EntityRepository
{
/**
- * Retrieves unread entries for a user
+ * Count all tags per user.
*
* @param int $userId
- * @param int $firstResult
- * @param int $maxResults
+ * @param int $cacheLifeTime Duration of the cache for this query
*
- * @return Paginator
+ * @return int
*/
- public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
+ public function countAllTags($userId, $cacheLifeTime = null)
{
- $qb = $this->createQueryBuilder('e')
- ->setFirstResult($firstResult)
- ->setMaxResults($maxResults)
- ->leftJoin('e.user', 'u')
- ->where('e.isArchived = false')
- ->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);
-
- return $paginator;
- }
-
- /**
- * Retrieves read entries for a user
- *
- * @param int $userId
- * @param int $firstResult
- * @param int $maxResults
- *
- * @return Paginator
- */
- public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
- {
- $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)
- ->orderBy('e.createdAt', 'desc')
- ->getQuery();
-
- $paginator = new Paginator($qb);
+ if (null !== $cacheLifeTime) {
+ $query->useQueryCache(true);
+ $query->useResultCache(true);
+ $query->setResultCacheLifetime($cacheLifeTime);
+ }
- return $paginator;
+ return count($query->getArrayResult());
}
/**
- * Retrieves starred entries for a user
+ * Find all tags per user.
*
* @param int $userId
- * @param int $firstResult
- * @param int $maxResults
*
- * @return Paginator
+ * @return array
*/
- public function findStarredByUser($userId, $firstResult, $maxResults = 12)
+ public function findAllTags($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)
- ->orderBy('e.createdAt', 'desc')
- ->getQuery();
-
- $paginator = new Paginator($qb);
-
- return $paginator;
+ 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();
}
/**
- * Find Entries
+ * Used only in test case to get a tag for our entry.
*
- * @param int $userId
- * @param bool $isArchived
- * @param bool $isStarred
- * @param string $sort
- * @param string $order
- *
- * @return array
+ * @return Tag
*/
- public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
+ public function findOneByEntryAndTagLabel($entry, $label)
{
- $qb = $this->createQueryBuilder('e')
- ->where('e.user =:userId')->setParameter('userId', $userId);
-
- if (null !== $isArchived) {
- $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
- }
-
- if (null !== $isStarred) {
- $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
- }
-
- if ('created' === $sort) {
- $qb->orderBy('e.createdAt', $order);
- } elseif ('updated' === $sort) {
- $qb->orderBy('e.updatedAt', $order);
- }
-
- return $qb
+ 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()
- ->getResult();
+ ->getSingleResult();
}
}