2012-03-29 112 views
1

我在找这个,还有一些相关的问题,但没有人给我我需要的答案。我有一个日期字段的实体,我需要选择那些谁是超过7天从现在开始:在mysql/symfony2中比较日期

$query = $repository->createQueryBuilder('rf') 
        ->where('rf.sendDate >='.new \DateTime('-7 days')) 
        ->getQuery(); 

我收到此错误:

Catchable Fatal Error: Object of class DateTime could not be converted to string 

让我感到奇怪的是,为什么它假定rf.sendDate是一个字符串,何时被定义为实体中的DateTime对象?我怎么能比较这个?

任何解释真的很感激。

回答

6

你应该使用的参数是:

$query = $repository->createQueryBuilder('rf') 
      ->where('rf.sendDate >= :ts') 
       ->setParameter('ts', new \DateTime('-7 days')) 
      ->getQuery(); 
+1

超快速,超答案。谢谢! – Manu 2012-03-29 09:21:08