Search code examples
symfonydoctrine-ormdql

Getting entities which date is between of requested dates in Symfony2


As the title suggested, how can I get the result of this query:

$query = $repository->createQueryBuilder('p')
    ->where('p.sellDate > '.$startDate->format('Y/m/d'))
    ->andWhere('p.sellDate < '.$endDate->format('Y/m/d'))
    ->getQuery();

currently no result is given back. I've changed this code many times but didn't get the currect result.


Solution

  • Let Doctrine sort out the date objects with:

        $query = $repository->createQueryBuilder('p')
            ->where('p.sellDate > :startDate')
            ->andWhere('p.sellDate < :endDate')
            ->setParameter('startDate', $startDate)
            ->setParameter('endDate', $endDate)
            ->getQuery()
        ;
    
        $sells = $query->getResult();