php – 在doctrine 2中选择日期之间的条目

我会疯狂的这个最小的错误,我没有得到解决。我想选择两天之间的条目,下面的例子ilustrate所有我的失败:

opt 1。

$qb->where('e.fecha > ' . $monday->format('Y-m-d'));
$qb->andWhere('e.fecha < ' . $sunday->format('Y-m-d'));

结果(0条):

SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2 
FROM reservacion r0_ 
WHERE (r0_.fecha > 2012 - 07 - 16) AND (r0_.fecha < 2012 - 07 - 22)

opt 2

$qb->add('where', 'e.fecha between 2012-01-01 and 2012-10-10');

结果(0条):

SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2 
FROM reservacion r0_ WHERE r0_.fecha 
BETWEEN 2012 - 01 - 01 AND 2012 - 10 - 10

这是我的表与当前条目:

id      fecha            cliente
1   2012-07-16 00:00:00    2    
2   2012-07-16 13:00:00    4    
3   2012-07-22 23:00:00    4

编辑1

为了评估sql以避免疑惑,我运行这个查询:

$qb->where('e.fecha > ' . $sunday->format('Y-m-d'));

结果(3条):

SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2 

所以,看起来像sql不是问题。
    FROM reservacion r0_
    WHERE r0_.fecha> 2012 – 07

您可以…

$qb->where('e.fecha BETWEEN :monday AND :sunday')
   ->setParameter('monday', $monday->format('Y-m-d'))
   ->setParameter('sunday', $sunday->format('Y-m-d'));

要么…

$qb->where('e.fecha > :monday')
   ->andWhere('e.fecha < :sunday')
   ->setParameter('monday', $monday->format('Y-m-d'))
   ->setParameter('sunday', $sunday->format('Y-m-d'));
http://stackoverflow.com/questions/11553183/select-entries-between-dates-in-doctrine-2

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:php – 在doctrine 2中选择日期之间的条目