Symfony 2.3 Gedmo doctrine extensions translatable caching

2.5k views Asked by At

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

1

There are 1 answers

3
Karol Wojciechowski On BEST ANSWER

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 to "getResult"

Performing getResult changes hydration mode. Have look on 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 didn't changes hydration mode

public function getOneOrNullResult($hydrationMode = null)

If 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.