mirror of
https://github.com/wallabag/wallabag.git
synced 2025-02-22 13:36:18 +00:00
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:
parent
9d7dd6b0d2
commit
faa86e06ba
4 changed files with 46 additions and 46 deletions
|
@ -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');
|
||||
|
||||
|
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in a new issue