2013-01-24 67 views
0

我有這樣的SQL查詢:從原材料的SQL查詢的symfony 2 SQL查詢

SELECT * from gift WHERE NOW() >= `validbegin` AND NOW() <= `validend` ORDER BY `points`n ASC 

我需要將其轉換爲一個Symfony的2查詢到的數據提取到一個對象調用禮品。到目前爲止,我有這樣的:

$query = $giftRepository->createQueryBuilder('p') 
     ->where('NOW() >= validbegin AND NOW() <= p.validend') 
     ->orderBy('p.points', 'ASC') 
     ->getQuery(); 
    $gifts = $query->getResult(); 

但是,這給了我:

[Syntax Error] line 0, col 53: Error: Expected known function, got 'NOW'

任何想法?

ps。也試過p.NOW()

+0

什麼樣的領域是,日期時間? – mpm

回答

1

試試這個:

$query = $giftRepository->createQueryBuilder('p') 
    ->where(':now >= validbegin AND :now <= p.validend') 
    ->setParameter('now', new \DateTime()) 
    ->orderBy('p.points', 'ASC') 
    ->getQuery(); 
$gifts = $query->getResult(); 

您產生從PHP的字段,然後主義會自動將其轉換爲一個MySQL時間戳