Symfony 2.3 Gedmo理论扩展可翻译缓存 [英] Symfony 2.3 Gedmo doctrine extensions translatable caching

查看:143
本文介绍了Symfony 2.3 Gedmo理论扩展可翻译缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Gedmo Doctrine扩展程序

全部效果很好,翻译缓存除外。

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()返回 \Doctrine\ORM\Query

但是翻译没有被缓存。在分析器中,我看到这样的查询:

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 = ?

分析器中的所有查询都是从 ext_translations 。如何使用已翻译的字符串缓存结果?

And all the queries in profiler are to get results from ext_translations. How can I cache the result already with the translated strings?

尝试使用Array Hydration和memCache,但是结束了更多的错误,因为我的结果项目已经上传了可以不要被序列化或者是某种东西。无论如何,我最终会重写大部分代码。

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.

任何帮助都不胜感激。

修改

我已经尝试了Karol Wojciechowski的答案,它解决了部分问题。当我使用 getOneOrNullResult()但不与 getResult()时缓存。以下是一些代码。

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.

在一项服务中:

$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();

渠道存储库:

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();
}

如果我更改为 findActiveChannelsByGroupId($ id,1 )(通知限制参数),它仍然不缓存,但是如果我更改为 getOneOrNullResult(),则查询缓存

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

推荐答案

我们的工作代码:

    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);
}

编辑:
如果你想要getResult

And if you want to "getResult"

执行getResult更改水化模式。查看AbstractQuery类方法:

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);
    }

它适用于 getOneOrNullResult 因为它没有改变水化模式

It works with getOneOrNullResult because it didn't changes hydration mode

public function getOneOrNullResult($hydrationMode = null)

如果要缓存可翻译查询,您应该在执行期间更改水化模式getResult方法为 TranslationWalker :: HYDRATE_OBJECT_TRANSLATION

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.

这篇关于Symfony 2.3 Gedmo理论扩展可翻译缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆