Using `DATE()` in Doctrine Querybuilder

Daniel W. picture Daniel W. · Sep 23, 2014 · Viewed 21.1k times · Source

I need to get all rows where DATE(a.when) matches the string 2014-09-30.

$builder = $this->em->createQueryBuilder();
$builder->select('a')
        ->from('Entity\Appointment', 'a')
        ->andWhere('a.when = :date')
        ->setParameter('date', $date);

a.when is a full DATETIME; :date is only a string (in DATE format).

The following and variations didn't work:

        ->andWhere('DATE(a.when) = :date')

Error: Expected known function, got 'DATE'

What's the correct usage here?

Answer

Daniel W. picture Daniel W. · Sep 23, 2014

Thanks to andy, using this now:

$builder = $this->em->createQueryBuilder();
$builder->select('a')
        ->from('Entity\Appointment', 'a')
        ->andWhere('a.when >= :date_start')
        ->andWhere('a.when <= :date_end')
        ->setParameter('date_start', $date->format('Y-m-d 00:00:00'))
        ->setParameter('date_end',   $date->format('Y-m-d 23:59:59'));