在Symfony2中,我应该使用实体还是自定义存储库 [英] In Symfony2, should I use an Entity or a custom Repository

查看:61
本文介绍了在Symfony2中,我应该使用实体还是自定义存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的Web应用程序,并希望在设计计划方面有所帮助.

I am creating a new web app and would like some help on design plans.

我有存储"对象,每个对象都有许多消息"对象.我想显示一个显示该商店消息的商店页面.使用Doctrine,我已经使用 http://symfony.com/doc/current/book映射了OneToMany /doctrine.html

I have "store" objects, and each one has a number of "message" objects. I want to show a store page that shows this store's messages. Using Doctrine, I have mapped OneToMany using http://symfony.com/doc/current/book/doctrine.html

但是,我想按相反的时间顺序显示消息.所以我加了一个:

However, I want to show messages in reverse chronological order. So I added a:

* @ORM\OrderBy({"whenCreated" = "DESC"})

仍然要调用"store"对象,然后调用

Still I am calling the "store" object, then calling

$store->getMessages();

现在,我想显示已验证"的消息.此时,我不确定如何使用@ORM进行此操作,因此我想我需要一个自定义存储库层.

Now I want to show messages that have been "verified". At this point, I am unsure how to do this using @ORM so I was thinking I need a custom Repository layer.

我的问题有两个:

  1. 首先,我可以使用Entity @ORM框架来做到这一点吗?
  2. 第二,包装此数据库查询的正确方法是什么?
  1. First, can I do this using the Entity @ORM framework?
  2. And second, which is the correct way to wrap this database query?

我知道我最终想要SQL SELECT * FROM message WHERE verified=1 AND store_id=? ORDER BY myTime DESC,但是如何使它成为"Symfony2方式"?

I know I eventually want the SQL SELECT * FROM message WHERE verified=1 AND store_id=? ORDER BY myTime DESC but how to make this the "Symfony2 way"?

推荐答案

对于您的问题的第1部分...从技术上讲,我认为您可以做到这一点,但我认为您无法在有效的方法,或不违背良好做法的方法(例如,将实体管理器注入您的实体).

For part 1 of your question... technically I think you could do this, but I don't think you'd be able to do it in an efficient way, or a way that doesn't go against good practices (i.e. injecting the entity manager into your entity).

您的问题很有趣,因为乍一看,我还会考虑使用$store->getMessages().但是由于您的自定义条件,我认为最好使用Messages的自定义存储库类.然后,您可能会使用

Your question is an interesting one, because at first glance, I would also think of using $store->getMessages(). But because of your custom criteria, I think you're better off using a custom repository class for Messages. You might then have methods like

$messageRepo->getForStoreOrderedBy($storeId, $orderBy)

$messageRepo->getForStoreWhereVerified($storeId).

现在,您可以使用$store->getMessagesWhereVerified()之类的方法从Store实体中执行此操作,但我认为您会污染Store实体,尤其是在您需要越来越多的自定义方法的情况下.我认为通过将它们保存在消息存储库中,可以以一种更简洁的方式分离您的关注点.另外,使用Message存储库时,您可以通过 not 来保存自己的查询,而无需先获取Store对象,因为您只需要查询Message表并在WHERE子句中使用其store_id

Now, you could do this from the Store entity with methods like $store->getMessagesWhereVerified() but I think that you would be polluting the store entity, especially if you need more and more of these custom methods. I think by keeping them in a Message repository, you're separating your concerns in a cleaner fashion. Also, with the Message repository, you might save yourself a query by not needing to first fetch your Store object, since you would only need to query to Message table and use its store_id in your WHERE clause.

希望这会有所帮助.

这篇关于在Symfony2中,我应该使用实体还是自定义存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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