Symfony 2.3 Extending Gedmo Doctrine for Translatable Caching
I am using Gedmo Doctrine Extensions
So far everything works well except for caching translations.
$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 translations are not cached. In the profiler, I see requests like this:
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 queries in the profiler should get results from ext_translations
. How can I cache the result already with translated strings?
Tried using Array Hydration and memCache but ended up messing up more because there was a media loaded in my result element that could not be serialized or something. Anyway, I would rewrite most of the code.
Any help is appreciated.
Edit
I tried Karol Wojciechowski's answer and it solved part of the problem. It is cached when I use getOneOrNullResult()
, but not with getResult()
. Here's the code.
In 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();
Channel 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 go to findActiveChannelsByGroupId($id, 1)
(notification limit option) it still doesn't getOneOrNullResult()
cache , but if I go to the request is cached
source to share
Our working code:
public function addTranslationWalkerToQuery($query, $request)
{
$config = $this->container->get('doctrine')->getManager()->getConfiguration();
if ($config->getCustomHydrationMode(TranslationWalker::HYDRATE_OBJECT_TRANSLATION) === null) {
$config->addCustomHydrationMode(
TranslationWalker::HYDRATE_OBJECT_TRANSLATION,
'Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'
);
}
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$query->setHint(
\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
$request->getLocale() // take locale from session or request etc.
);
$query->setHydrationMode(TranslationWalker::HYDRATE_OBJECT_TRANSLATION);
$query->setHint(Query::HINT_REFRESH, true);
}
EDIT: And if you want "getResult"
Executing getResult changes the hydration mode. Take a look at the AbstractQuery class method:
/**
* Gets the list of results for the query.
*
* Alias for execute(null, $hydrationMode = HYDRATE_OBJECT).
*
* @param int $hydrationMode
*
* @return array
*/
public function getResult($hydrationMode = self::HYDRATE_OBJECT)
{
return $this->execute(null, $hydrationMode);
}
It works with getOneOrNullResult
because it does not change the hydration regimen
public function getOneOrNullResult($hydrationMode = null)
If you want to cache translatable requests, you must change the hydration mode while the getResult method is running before TranslationWalker::HYDRATE_OBJECT_TRANSLATION
.
Personally, I will include this method in a service that will handle everything related to translations.
source to share