Symfony表单EntityType缓存

编程入门 行业动态 更新时间:2024-10-27 15:21:36
本文介绍了Symfony表单EntityType缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Symfony v3.2中,我使用的表单具有多个EntityType字段,其中包含数百个选项-每个选项都是一个相对较大的对象。 因为它们不经常更改,所以我想在Symfony中使用一些缓存,将它们加载一次,然后继续用它来输入EntityType。

In Symfony v3.2, I'm using a form with several EntityType fields, which have hundreds of options - and each option is a relatively big object. Since they don't change so often, I'd like to use some Cache in Symfony, to load them once and just keep feeding the EntityType with it.

我已经缩小了馈送数据的大小,仅提取了我需要的字段,然后将其保存到缓存中。

I've already cut down the size of the data that's feeding it, by pulling just the fields that I need, and then saved that into a cache.

何时我从缓存中提取数据-我无法使用 choice_list 直接将其提供给EntityType,因为它已与ObjectManager分离,并且出现了错误(实体已通过选择字段中的必须进行管理。)。 要重新附加它,我可以使用ObjectManager-> merge(),但这意味着要为每个要重新合并并重新附加到Manager的项目调用DB。达到了缓存的目的。

When I pull the data from cache - I cannot feed it directly to EntityType with a choice_list, because it gets detached from ObjectManager, and I get an error ("Entities passed to the choice field must be managed"). To re-attach it, I could use the ObjectManager->merge(), but then it means doing a call to DB for each item being re-merged and re-attached to the Manager. That beats the purpose of Caching.

在这种情况下进行处理的最佳方法是什么?只是完全从Form中丢失EntityType(对于速度敏感的页面)并选择ChoiceType(这还包括更改代码许多部分的逻辑)?还有比这更好的东西吗?

What is the best way to proceed in this scenario? Just lose the EntityType completely from the Form (for speed-sensitive pages) and go with the ChoiceType (which would also include changing the logic in many parts of code)? Anything nicer than that?

到目前为止,我在SO或其他地方都找不到解决方案。

So far I didn't find anything near to solution on SO or elsewhere.

推荐答案

我在分析表单时遇到了相同的问题。我面临的问题之一是添加使用 QueryBuilder 进行第二级缓存非常容易,但是 EntityRepository 方法却不这样做。

I've ran into the same question when profiling my forms. One of the problems I faced is that adding Second level caching is very easy when using the QueryBuilder, but the EntityRepository methods don't use that cache out of the box.

该解决方案实际上非常简单。只需在 query_builder 中添加一些缓存设置即可。这里有一个来自Symfony文档的示例:

The solution was actually pretty simple. Just add some cache settings to your query_builder. Here an example from the Symfony documentation:

$builder->add('users', EntityType::class, array( 'class' => User::class, 'query_builder' => function (EntityRepository $er) { return $er->createQueryBuilder('u') //add something like this ->setCacheable(true) ->setCacheMode(Cache::MODE_NORMAL) ->setCacheRegion('default') ->orderBy('u.username', 'ASC'); }, 'choice_label' => 'username', ));

别忘了向您的实体添加二级缓存:

Don't forget to add the second level cache to your entity:

/** * @ORM\Entity * @ORM\Cache(region="default", usage="NONSTRICT_READ_WRITE") */ class User { }

更多推荐

Symfony表单EntityType缓存

本文发布于:2023-10-10 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   缓存   Symfony   EntityType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!