Symfony2:在实体或控制器中使用业务(存储库)逻辑的最佳方法 [英] Symfony2: best approach to use business (repository) logic in entity or controller

查看:155
本文介绍了Symfony2:在实体或控制器中使用业务(存储库)逻辑的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个设计问题,涉及到哪里放一些业务逻辑。



我有三个实体,Event,TicketOrder和Ticket。一个事件有很多TicketOrders和一个TicketOrder有很多门票。



在我的模板中,我必须显示一个事件有多少票。我想到了最好的方法来实现这一点,并没有得到一个很好的解决方案。我已经尝试过:



1)在事件实体中创建一个私有成员ticketCount,使用setTicketsCount和getTicketsCount方法。使用LifeCycleCallback'PostLoad'创建一个'loadTicketsCount'方法来访问TicketRepository方法'findByEvent'。这是不可能的,因为我无法访问实体类中的存储库。



2)在用于显示事件的操作中,我可以访问Ticket Repository手动设置事件ticketCount属性。我不知道这是否是一个很好的方法,因为如果我的动作列出了很多事件,我将不得不循环所有的事件,然后对每个事件进行存储库调用。



我真的不知道实现这一点的最佳方法,真的很感激有人可以帮助我。



谢谢! ;)

解决方案

当您使用findAll,findBy或findBy *方法的doctrine实体库时,返回一个简单的php数组,其中包含实体对象。



数组类实现可数接口。所以使用枝条长度过滤器

  {{ticketOrder.tickets | length}} 
/ pre>

你在数组上执行一个简单的php count()



实际上,执行计数查询是有意义的,因为您已经将内存中的结果。因此,计数结果并从内存中检索结果似乎更有效率,因为当您访问关联时,它们被完全加载到内存中。






然而,实体之间的关联可以变得相当大。所以想象你有数十个实体的关联。你不会把这些东西都加载在一起,一直在记忆中。所以在Doctrine 2.1中,您可以将关联注释为 Extra Lazy 。如果您在这种情况下执行此操作,则在调用上述枝条筛选器时执行计数查询。但是结果并没有保存在内存中。



http://docs.doctrine-project.org/en/2.0.x/tutorials/extra-lazy-associations.html






根据您最新的评论:



我可以想象一个这样做的方法。在模板中,您可以使用render语句调用控制器的操作,如

  {%render YourMainBundle:getTickets with {'event_id' event.id}%} 

在此操作中,您可以调用查找所有相关联的票据的查询到某个事件。此操作必须返回html,例如填充数据的模板。


I'm having a design issue in my project, related to where put some business logic.

I have three entities, Event, TicketOrder and Ticket. One Event has a lot of TicketOrders and one TicketOrder has a lot of Tickets.

In my template, I have to show how many tickets an Event has. I've thinking of the best approach to achieve this and didn't get a good solution. I've tried this:

1) Create a private member 'ticketsCount' in Event entity, with setTicketsCount and getTicketsCount method. Create a 'loadTicketsCount' method with a LifeCycleCallback 'PostLoad', to access the TicketRepository method 'findByEvent'. This was impossible because I can't access repository in an entity class.

2) In the action that will be used to display the Event, I can access Ticket Repository and set event 'ticketsCount' property manually. I don't know if it is a good approach because if my action is listing a lot of events I'll have to loop trough all events and make a repository call to each of then.

I really don't know the best approach to achieve this and will really appreciate if someone can help me.

Thanks! ;)

解决方案

When you use findAll, findBy or findBy* methods of doctrine entity repository, a simple php array is returned containing the entity objects.

The array class implements countable interface. So using twigs length filter

{{ ticketOrder.tickets|length }}

you perform a simple php count() on the array.

Actually it makes now sense to perform a count query, because you already have the result in memory. So it seems more efficient to count the result and retrieve it from memory, because when you access associations they are completely loaded into memory.


However associations between entities can get pretty large. So imagine you have associations with hundred thousands of entities. You won't those entites to be loaded all together and kept in memory all the time. So in Doctrine 2.1 you can annotate an association as Extra Lazy. If you do so in your case a count query is performed when you call the above twig filter. But the result is not kept in memory.

http://docs.doctrine-project.org/en/2.0.x/tutorials/extra-lazy-associations.html


According to your latest comment:

I can imagine one way to do this. In a template you can call a controller's action with the render statement like

{% render YourMainBundle:getTickets with { 'event_id' : event.id } %}

and in this action you can call a query that looks for all tickets associated to the certain event. This action has to return html, e.g. an template filled with data.

这篇关于Symfony2:在实体或控制器中使用业务(存储库)逻辑的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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