现在选择startDate和endDate

时间:2016-04-20 22:24:45

标签: symfony doctrine-orm

我想选择现在位于startDate和endDate之间的条目,但它不起作用,我对此查询没有错误

$qb = $this->createQueryBuilder('c')
        ->Where(':now BETWEEN :startDate AND :endDate')
        ->setParameter('startDate', 'c.startDate')
        ->setParameter('endDate', 'c.endDate')
        ->setParameter('now', new \Datetime())
        ->orderBy('c.id', 'DESC');

    return $qb->getQuery()->getResult();

我已经更改了这样的查询,现在可以使用了:

$qb = $this->createQueryBuilder('c')
        ->Where(':now BETWEEN c.startDate AND c.endDate')
        ->setParameter('now', new \Datetime())
        ->orderBy('c.id', 'DESC');
return $qb->getQuery()->getResult();

1 个答案:

答案 0 :(得分:2)

$repository = $this->getDoctrine()->getRepository('AppBundle:Product');

$qb = $repository->createQueryBuilder('c')
        ->where('c.startDate < :now')
        ->andWhere('c.endDate > :now')
        ->setParameter('now', new \Datetime())
        ->orderBy('c.id', 'DESC');

    return $qb->getQuery()->getResult();