Fix tags count in menu

Move enable cache for Tag in the Entity because function `find*` should return result and not a Query
This commit is contained in:
Jeremy Benoist 2016-09-25 11:21:13 +02:00
parent 9d7dd6b0d2
commit faa86e06ba
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
4 changed files with 46 additions and 46 deletions

View file

@ -322,9 +322,7 @@ class WallabagRestController extends FOSRestController
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
->findAllTags($this->getUser()->getId())
->getQuery()
->getResult();
->findAllTagsWithEntries($this->getUser()->getId());
$json = $this->get('serializer')->serialize($tags, 'json');

View file

@ -84,16 +84,11 @@ class TagController extends Controller
{
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
->findAllTags($this->getUser()->getId())
->getQuery()
->getResult();
->findAllTagsWithEntries($this->getUser()->getId());
return $this->render(
'WallabagCoreBundle:Tag:tags.html.twig',
[
'tags' => $tags,
]
);
return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
'tags' => $tags,
]);
}
/**
@ -127,13 +122,10 @@ class TagController extends Controller
}
}
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,
]);
}
}

View file

@ -7,17 +7,45 @@ use Doctrine\ORM\EntityRepository;
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, $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 findAllTags($userId)
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();
}
/**

View file

@ -85,10 +85,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
->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());
}
/**
@ -104,30 +105,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
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';