I have following question:
我有以下问题:
Imagine that we have an entity, e.g. Event.
想象一下,我们有一个实体,例如事件。
In EventRepository class I've added some methods for querying some kind of Events.
在EventRepository类中,我添加了一些查询某种事件的方法。
In the EventController I've got a collection of Events. Last step - display all these Events in the template. Basically there is nothing difficult:
在EventController中,我收到了一系列事件。最后一步 - 在模板中显示所有这些事件。基本上没有什么困难:
{% for event in events %}...{% endfor %}
But I have one additional condition: I need to display Users who goes to the each Event (e.g. 10 users for each). Event linked to the User with Many-to-Many association. Ok, I've added method to the EventRepository to find event participants. But I have no access to the EventRepository in Twig template... :(
但我还有一个附加条件:我需要显示转到每个事件的用户(例如每个用户10个用户)。与具有多对多关联的用户链接的事件。好的,我已经在EventRepository中添加了方法来查找事件参与者。但我无法访问Twig模板中的EventRepository ...... :(
I see three options:
我看到三个选项:
- (quick and tricky) Get participants directly by the Entity:
{% for event in events %}{% set participants = event.participants %}{% endfor %}
. This works, but when we'll have participants count 1000+? Afaik event->getParticipants() will querying all 1000+ Users at once that will break my page. - pass EventRepository instance into temlpate and query 10 users for each event, something like this:
{% for event in events %}{% set participants = eventRepository.getEventParticipants(event.id, max) %}{% endfor %}
. This looks more correct but looks like this breaks core MVC concepts - querying database within templates. - Loop events in the controller and prepare Participants list for each. Look ugly (at least dooble loop for events and more code in controller), but more or less right (and what need to do when new conditions appears?).
(快速而棘手)实体直接与参与者联系:{%for events in events%} {%set participant = event.participants%} {%endfor%}。这有效,但是当我们有参与者数量超过1000? Afaik event-> getParticipants()将立即查询所有1000多个用户,这将破坏我的页面。
将EventRepository实例传递给temlpate并为每个事件查询10个用户,如下所示:{%for events in events%} {%set participant = eventRepository.getEventParticipants(event.id,max)%} {%endfor%}。这看起来更正确,但看起来这打破了核心MVC概念 - 在模板中查询数据库。
在控制器中循环事件并为每个事件准备参与者列表。看起来很丑陋(至少可以在事件中使用dooble循环,在控制器中使用更多代码),但或多或少是正确的(以及新条件出现时需要做什么?)。
Can you take any advise for this case? How I need to query N-N relation and where in my code?
你能为这个案子提出任何建议吗?我如何查询N-N关系以及我的代码中的位置?
1 个解决方案
#1
1
As long as your relationships are correctly set on your Event entity you should be able to lazy load the users when they're required. So for example when iterating over your events you should be able to call..
只要您的事件实体上的关系设置正确,您就应该能够在需要时延迟加载用户。因此,例如在迭代您的事件时,您应该能够调用..
$event->getUsers();
Doctrine2 is really well suited for these exact types of operations (lazy loading the content when its required).
Doctrine2非常适合这些确切类型的操作(在需要时延迟加载内容)。
Make sure you have a valid relation between Events and Users and write a getter in your Events entity to pull out all users as either an array, or using Doctrine's ArrayCollection class.
确保事件和用户之间存在有效关系,并在Events实体中编写getter,以将所有用户作为数组或使用Doctrine的ArrayCollection类提取出来。
#1
1
As long as your relationships are correctly set on your Event entity you should be able to lazy load the users when they're required. So for example when iterating over your events you should be able to call..
只要您的事件实体上的关系设置正确,您就应该能够在需要时延迟加载用户。因此,例如在迭代您的事件时,您应该能够调用..
$event->getUsers();
Doctrine2 is really well suited for these exact types of operations (lazy loading the content when its required).
Doctrine2非常适合这些确切类型的操作(在需要时延迟加载内容)。
Make sure you have a valid relation between Events and Users and write a getter in your Events entity to pull out all users as either an array, or using Doctrine's ArrayCollection class.
确保事件和用户之间存在有效关系,并在Events实体中编写getter,以将所有用户作为数组或使用Doctrine的ArrayCollection类提取出来。