Doctrine Mongodb ODM和DateTime查询(Doctrine Mongodb ODM and DateTime query)

编程入门 行业动态 更新时间:2024-10-28 00:14:46
Doctrine Mongodb ODM和DateTime查询(Doctrine Mongodb ODM and DateTime query)

我可以在这个问题上使用一些帮助。 我正在使用Symfony2 + mongodb + doctrine创建一个应用程序。 我只想使用Doctrine ODM查询过去5分钟内登录过的所有用户。 我有一个User集合,其中包含一个名为date_last_login的日期字段。

所以我尝试使用这样的querybuilder:

<?php // Creating a DateTime object and susbtract 5 min from now // local time is 15:40:05, timezone: 'Europe/Paris' $_dateTime = new \DateTime(); $_interval5Min = new \DateInterval('PT5M'); $_dateTime->sub($_interval5Min); $query = $this->createQueryBuilder('User') ->field('date_last_login')->gte($_dateTime) ->getQuery(); ->execute();

当我使用symfony2 profiler查看汇编的查询时,我得到的是:

db.User.find({ "date_last_login": { "$gte": new Date("Fri, 23 Dec 2011 15:30:05 +0100") } });

看起来很好,除了日期提前10分钟而不是5分钟? 我只是不明白。 如果我转储我的php DateTime对象,日期是正确的:2011-12-23 15:35:05(15:40之前的五分钟)。

所以我尝试组装相同的查询而不减少任何分钟,这一次,一切都很好:

<?php // local time is 15:50:00 $query = $this->createQueryBuilder('User') ->field('date_last_login')->gte(new \DateTime()) ->getQuery(); ->execute(); // query is ok: db.User.find({ "date_last_login": { "$gte": new Date("Fri, 23 Dec 2011 15:50:00 +0100") } });

我究竟做错了什么 ? 感谢您的帮助!

I could use some help on this problem. I'm creating an application using Symfony2 + mongodb + doctrine. I just want to use Doctrine ODM to query all the users who have been logged in the last 5 minutes. I have a User collection with a date field called date_last_login.

So I try to use the querybuilder like that:

<?php // Creating a DateTime object and susbtract 5 min from now // local time is 15:40:05, timezone: 'Europe/Paris' $_dateTime = new \DateTime(); $_interval5Min = new \DateInterval('PT5M'); $_dateTime->sub($_interval5Min); $query = $this->createQueryBuilder('User') ->field('date_last_login')->gte($_dateTime) ->getQuery(); ->execute();

When I looked at the assembled query using symfony2 profiler, here is what I got:

db.User.find({ "date_last_login": { "$gte": new Date("Fri, 23 Dec 2011 15:30:05 +0100") } });

It seems fine except that the date is 10 minutes earlier rather than 5 minutes? I just don't get it. If I dump my php DateTime object, date is correct: 2011-12-23 15:35:05 (five minutes before 15:40).

So I tried to assemble the same query without substracting any minutes and this time, everything is fine:

<?php // local time is 15:50:00 $query = $this->createQueryBuilder('User') ->field('date_last_login')->gte(new \DateTime()) ->getQuery(); ->execute(); // query is ok: db.User.find({ "date_last_login": { "$gte": new Date("Fri, 23 Dec 2011 15:50:00 +0100") } });

What am I doing wrong ? Thank you for your help!

最满意答案

这可能是由于这个PHP bug在5.3.3中修复了:

https://bugs.php.net/bug.php?id=50916

This is likely due to this PHP bug which was fixed in 5.3.3:

https://bugs.php.net/bug.php?id=50916

更多推荐

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

发布评论

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

>www.elefans.com

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