• <bdo id='jtnn7'></bdo><ul id='jtnn7'></ul>

  1. <tfoot id='jtnn7'></tfoot>

    <small id='jtnn7'></small><noframes id='jtnn7'>

    1. <legend id='jtnn7'><style id='jtnn7'><dir id='jtnn7'><q id='jtnn7'></q></dir></style></legend>

      <i id='jtnn7'><tr id='jtnn7'><dt id='jtnn7'><q id='jtnn7'><span id='jtnn7'><b id='jtnn7'><form id='jtnn7'><ins id='jtnn7'></ins><ul id='jtnn7'></ul><sub id='jtnn7'></sub></form><legend id='jtnn7'></legend><bdo id='jtnn7'><pre id='jtnn7'><center id='jtnn7'></center></pre></bdo></b><th id='jtnn7'></th></span></q></dt></tr></i><div id='jtnn7'><tfoot id='jtnn7'></tfoot><dl id='jtnn7'><fieldset id='jtnn7'></fieldset></dl></div>

    2. Symfony 2.3 Gedmo 学说扩展可翻译缓存

      时间:2023-08-19
    3. <i id='3byJY'><tr id='3byJY'><dt id='3byJY'><q id='3byJY'><span id='3byJY'><b id='3byJY'><form id='3byJY'><ins id='3byJY'></ins><ul id='3byJY'></ul><sub id='3byJY'></sub></form><legend id='3byJY'></legend><bdo id='3byJY'><pre id='3byJY'><center id='3byJY'></center></pre></bdo></b><th id='3byJY'></th></span></q></dt></tr></i><div id='3byJY'><tfoot id='3byJY'></tfoot><dl id='3byJY'><fieldset id='3byJY'></fieldset></dl></div>

          <small id='3byJY'></small><noframes id='3byJY'>

          • <bdo id='3byJY'></bdo><ul id='3byJY'></ul>

                  <tbody id='3byJY'></tbody>
                <legend id='3byJY'><style id='3byJY'><dir id='3byJY'><q id='3byJY'></q></dir></style></legend>
                <tfoot id='3byJY'></tfoot>

                本文介绍了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(DoctrineORMQuery::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\Translatable\Query\TreeWalker\TranslationWalker')
                            ->useResultCache(true, $cache_time, $cache_name)
                            ->getOneOrNullResult();
                

                findByIdFullData() 返回 DoctrineORMQuery
                但是翻译不会被缓存.在探查器中,我看到这样的查询:

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

                Profiler 中的所有查询都是从 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(
                        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);
                }
                

                如果你想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 方法期间将 hydration 模式更改为 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 学说扩展可翻译缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:学说2:在一对多的双向关系中,如何从反面保存? 下一篇:学说不允许 ResultSetMappingBuilder 工作

                相关文章

                  <bdo id='eUaLj'></bdo><ul id='eUaLj'></ul>
              1. <i id='eUaLj'><tr id='eUaLj'><dt id='eUaLj'><q id='eUaLj'><span id='eUaLj'><b id='eUaLj'><form id='eUaLj'><ins id='eUaLj'></ins><ul id='eUaLj'></ul><sub id='eUaLj'></sub></form><legend id='eUaLj'></legend><bdo id='eUaLj'><pre id='eUaLj'><center id='eUaLj'></center></pre></bdo></b><th id='eUaLj'></th></span></q></dt></tr></i><div id='eUaLj'><tfoot id='eUaLj'></tfoot><dl id='eUaLj'><fieldset id='eUaLj'></fieldset></dl></div>
                1. <small id='eUaLj'></small><noframes id='eUaLj'>

                  1. <legend id='eUaLj'><style id='eUaLj'><dir id='eUaLj'><q id='eUaLj'></q></dir></style></legend>

                    <tfoot id='eUaLj'></tfoot>