X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FTagRepository.php;h=e76878d49d6e7cc5bb06c0f2a1d8227ef2bec4bc;hb=8eeefc658c500c0839512ac2e259e194ab943d2c;hp=51f1cd42924b22a88eb879fd6b5b3b14cfff058f;hpb=47cadf36c8f7c20ba1edf26e184637d33a52cf35;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 51f1cd42..e76878d4 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -3,25 +3,68 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; -use Pagerfanta\Adapter\DoctrineORMAdapter; -use Pagerfanta\Pagerfanta; class TagRepository extends EntityRepository { /** - * Find Tags. + * Count all tags per user. * - * @param int $userId + * @param int $userId + * @param int $cacheLifeTime Duration of the cache for this query * - * @return array + * @return int */ - public function findTags($userId) + public function countAllTags($userId, $cacheLifeTime = null) { - $qb = $this->createQueryBuilder('t') - ->where('t.user =:userId')->setParameter('userId', $userId); + $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); + } - $pagerAdapter = new DoctrineORMAdapter($qb); + return count($query->getArrayResult()); + } - return new Pagerfanta($pagerAdapter); + /** + * 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(); } }