教义Mongodb ODM和DateTime查询 [英] Doctrine Mongodb ODM and DateTime query

查看:144
本文介绍了教义Mongodb ODM和DateTime查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 <?php 
//创建一个DateTime对象并从现在起5分钟
//本地时间是15:40:05,时区:'欧洲/ Paris'
$ _dateTime = new \DateTime();
$ _interval5Min = new \DateInterval('PT5M');
$ _dateTime-> sub($ _ interval5Min);

$ query = $ this-> createQueryBuilder('User')
- >字段('date_last_login') - > gte($ _ dateTime)
- > getQuery();
- > execute();

当我使用symfony2分析器查看汇编的查询时,这里是我所得到的:

  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 
//当地时间是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)}});

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

解决方案

这可能是由于此5.3.3中修复的PHP错误:



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


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!

解决方案

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

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

这篇关于教义Mongodb ODM和DateTime查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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