I'm using Gedmo Doctrine extensions
All works well so far, except for translations caching.
$entity = $repository
->findByIdFullData($id)
->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')
->useResultCache(true, $cache_time, $cache_name)
->getOneOrNullResult();
findByIdFullData()
returns \Doctrine\ORM\Query
But the translations aren't cached. In profiler I see queries like that:
SELECT
e0_.content AS content0,
e0_.field AS field1
FROM
ext_translations e0_
WHERE
e0_.foreign_key = ?
AND e0_.locale = ?
AND e0_.object_class = ?
And all the queries in profiler are to get results from ext_translations
. How can I cache the result already with the translated strings?
Tried using Array Hydration and memCache, but ended messing up more, because my result item has uploaded media file which can't be serialized or something. Anyway I would end up rewriting most of the code.
Any help is appreciated.
Edit:
I've tried Karol Wojciechowski's answer and it solved part of the problem. It caches when I use getOneOrNullResult()
but not with getResult()
. Here's some code.
In a service:
$query = $this->em
->getRepository('MainBundle:Channels')
->findActiveChannelsByGroupId($id);
$this->container->get('my.translations')->addTranslationWalkerToQuery($query, $this->request);
$channels = $query
->useResultCache(true, 900, '1__channels__active_by_group_1')
->getResult();
Channels repository:
public function findActiveChannelsByGroupId($group_id, $limit = null)
{
$rs = $this
->createQueryBuilder('c')
->select('c', 'm')
->leftJoin('c.media', 'm')
->leftJoin('c.group', 'g')
->where('c.active = 1')
->andWhere('g.id = :group_id')
->orderBy('c.sortOrder', 'asc')
->setParameter('group_id', $group_id)
->setMaxResults($limit);
return $rs->getQuery();
}
If I change to findActiveChannelsByGroupId($id, 1)
(notice limit param), it still doesn't cache, but then if I change to getOneOrNullResult()
, query gets cached
Our working code:
EDIT: And if you want to "getResult"
Performing getResult changes hydration mode. Have look on AbstractQuery class method:
It works with
getOneOrNullResult
because it didn't changes hydration modeIf you want to cache translatable queries you should change hydration mode during execution getResult method to
TranslationWalker::HYDRATE_OBJECT_TRANSLATION
.Personally I'll wrap this method into service which will handle everything associated with translations.