从原始sql查询到symfony 2 sql查询

时间:2013-01-24 20:40:51

标签: symfony doctrine

我有这个SQL查询:

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

我需要将其转换为Symfony 2查询以将数据提取到对象调用Gift。到目前为止,我有这个:

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

但那给了我:

  

[语法错误]第0行,第53栏:错误:预期的已知函数,得到   'NOW'

有什么想法吗?

PS。也尝试了p.NOW()

1 个答案:

答案 0 :(得分: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生成字段,然后doctrine会自动将其转换为mysql时间戳