简体   繁体   English

使用 Symfony / Doctrine 的 OneToMany 关系

[英]OneToMany relationship using Symfony / Doctrine

I am using Symfony and Doctrine and I am faced with an issue:我正在使用 Symfony 和 Doctrine ,我遇到了一个问题:

Let's imagine we have a User entity and a PhoneCall entity with a OneToMany relationship.假设我们有一个User实体和一个具有 OneToMany 关系的PhoneCall实体。

If, on a page, you have to display 2 lists: "Today" and "Yesterday" , displaying each User with its related PhoneCalls of the day (the PhoneCall entity has a created_at property) , how would you get this data and pass it to the view in a clean way?如果在一个页面上,您必须显示 2 个列表: “Today”“Yesterday” ,显示每个用户及其当天相关的电话呼叫(PhoneCall 实体具有 created_at 属性) ,您将如何获取此数据并传递它以干净的方式查看视图?

I have tried having a 'phonecalls' property on the User entity and querying the Users with their related PhoneCalls of 'today', then making another query fetching the Users with their related PhoneCalls of 'yesterday', but that does not work because when the second query fetches an object already fetched by the first query, It gives me that same object rather than another one with the PhoneCalls of the date I asked for.我已经尝试在用户实体上使用“电话呼叫”属性并使用“今天”的相关电话呼叫查询用户,然后进行另一个查询以获取用户的“昨天”相关电话呼叫,但这不起作用,因为当第二个查询获取第一个查询已经获取的 object,它给了我相同的 object 而不是另一个我要求的日期的电话。

I have also thought of making a query fetching all Users, then a separate query fetching all their PhoneCalls of today, then a separate query fetching all their PhoneCalls of yesterday, but then how would I combine all of these objects together in a clean way to pass it to the view?我也想过做一个查询来获取所有用户,然后一个单独的查询来获取他们今天所有的电话,然后一个单独的查询来获取他们昨天的所有电话,但是我将如何以一种干净的方式将所有这些对象组合在一起将其传递给视图?

Thank you!谢谢!

You can query the user's phone calls of "today" and "yesterday" in one query, and select only the phone calls with date == today in your view for the "today"s list, and the same thing for the "yesterday"s phone call list.您可以一次查询用户的“今天”和“昨天”的电话,并且select在“今天”列表中只有日期==今天的电话,对于“昨天”也是如此s 电话清单。

This method has one big advantage, it will be easier if tomorrow you want to add a list with element of "before yesterday".这种方法有一个很大的优势,如果明天你想添加一个包含“昨天之前”元素的列表会更容易。 You simply need to update your query and your view instead of creating a new query, updating the controller and the view.您只需更新查询和视图,而不是创建新查询,更新 controller 和视图。

The simplest and cleanest way is to make a method that queries the calls of a certain day in the model:最简单最干净的方法是在model中做一个查询某一天的调用的方法:

 phoneCallsFromDay ($ user_id, $ day) {
     return query here
 }

If today's call list is displayed very frequently you can create a "todayphonecalls" property in Users, which can call the above method to populate it.如果今天的通话列表显示非常频繁,你可以在用户中创建一个“todayphonecalls”属性,它可以调用上面的方法来填充它。

 $ today_calls = phoneCallsFrom ($ user_id, "today date here");

If it is not something that is displayed continuously, it is not recommended to load User with that data permanently.如果它不是连续显示的内容,则不建议永久加载用户该数据。

In any case, we will use the method that you create in the model to make the queries:无论如何,我们将使用您在 model 中创建的方法进行查询:

 $ today_calls = phoneCallsFrom ($ user_id, "today date here");
 $ yesterday_calls = phoneCallsFrom ($ user_id, "yesterday date here");

OR或者

 $ today_calls = User-> todayphonecalls;
 $ yesterday_calls = phoneCallsFrom ($ user_id, "yesterday date here");

if you have created that property in User.如果您在用户中创建了该属性。

And the variables are passed to the view.并将变量传递给视图。 So all the business is in the controller.所以所有的业务都在controller。

In addition, this method will serve to make any call from a view for any other date you want to show.此外,此方法将用于从视图中为您要显示的任何其他日期进行任何调用。

You can even improve it by adding two dates, if the second date is not entered the query will return only the values of the first date:您甚至可以通过添加两个日期来改进它,如果未输入第二个日期,则查询将仅返回第一个日期的值:

 phoneCallsFromDay ($ user_id, $ day_from, $ date_to = null) {
     if ($ date_to === null) {
         return query $ day_from
     }
       return query from $ day_from to $ date_to
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM