2012-03-29 113 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