Move enable cache for Tag in the Entity because function `find*` should return result and not a Query
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
- ->findAllTags($this->getUser()->getId())
- ->getQuery()
- ->getResult();
+ ->findAllTagsWithEntries($this->getUser()->getId());
$json = $this->get('serializer')->serialize($tags, 'json');
{
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
- ->findAllTags($this->getUser()->getId())
- ->getQuery()
- ->getResult();
-
- return $this->render(
- 'WallabagCoreBundle:Tag:tags.html.twig',
- [
- 'tags' => $tags,
- ]
- );
+ ->findAllTagsWithEntries($this->getUser()->getId());
+
+ return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
+ 'tags' => $tags,
+ ]);
}
/**
}
}
- return $this->render(
- 'WallabagCoreBundle:Entry:entries.html.twig',
- [
- 'form' => null,
- 'entries' => $entries,
- 'currentPage' => $page,
- ]
- );
+ return $this->render('WallabagCoreBundle:Entry:entries.html.twig',[
+ 'form' => null,
+ 'entries' => $entries,
+ 'currentPage' => $page,
+ ]);
}
}
class TagRepository extends EntityRepository
{
/**
- * Find Tags.
+ * Find all tags per user.
*
* @param int $userId
+ * @param int $cacheLifeTime Duration of the cache for this query
*
* @return array
*/
- public function findAllTags($userId)
+ public function findAllTags($userId, $cacheLifeTime = null)
+ {
+ $query = $this->createQueryBuilder('t')
+ ->select('t')
+ ->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 $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);
+ ->where('e.user = :userId')->setParameter('userId', $userId)
+ ->getQuery()
+ ->getResult();
}
/**
->groupBy('e.id')
->getQuery();
- $data = $this->enableCache($query)
- ->getArrayResult();
+ $query->useQueryCache(true);
+ $query->useResultCache(true);
+ $query->setResultCacheLifetime($this->lifeTime);
- return count($data);
+ return count($query->getArrayResult());
}
/**
return 0;
}
- $qb = $this->tagRepository->findAllTags($user->getId());
-
- $data = $this->enableCache($qb->getQuery())
- ->getArrayResult();
+ $data = $this->tagRepository->findAllTags($user->getId());
return count($data);
}
- /**
- * Enable cache for a query.
- *
- * @param Query $query
- *
- * @return Query
- */
- private function enableCache(Query $query)
- {
- $query->useQueryCache(true);
- $query->useResultCache(true);
- $query->setResultCacheLifetime($this->lifeTime);
-
- return $query;
- }
-
public function getName()
{
return 'wallabag_extension';