Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

php - Symfony 2.3 Gedmo doctrine extensions translatable caching

I'm using Gedmo Doctrine extensions
All works well so far, except for translations caching.

$entity = $repository
            ->findByIdFullData($id)
            ->setHint(DoctrineORMQuery::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\Translatable\Query\TreeWalker\TranslationWalker')
            ->useResultCache(true, $cache_time, $cache_name)
            ->getOneOrNullResult();

findByIdFullData() returns DoctrineORMQuery
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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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(
        DoctrineORMQuery::HINT_CUSTOM_OUTPUT_WALKER,
        'Gedmo\Translatable\Query\TreeWalker\TranslationWalker'
    );
    $query->setHint(
        GedmoTranslatableTranslatableListener::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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...