X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FTagRepository.php;h=9d127da7143b588e4b600088502ffd61c7dd654d;hb=114c55c0a6eade2ba6c53fe25f61cc58cca91620;hp=0f362f79ebb0efdc37d4a9f2599e4100e24723e6;hpb=0a018fe03980b35c9f7aca838e67a8efa43b7f2d;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 0f362f79..9d127da7 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -6,18 +6,61 @@ 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 with associated entries per user. + * + * @param int $userId + * + * @return array + */ + public function findAllTagsWithEntries($userId) + { + return $this->createQueryBuilder('t') + ->leftJoin('t.entries', 'e') + ->where('e.user = :userId')->setParameter('userId', $userId) + ->getQuery() + ->getResult(); + } + + /** + * 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(); } }