Aien Saidi
Aien Saidi

Reputation: 159

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.

Upvotes: 1

Views: 1180

Answers (2)

geoB
geoB

Reputation: 4716

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();

Upvotes: 2

Aien Saidi
Aien Saidi

Reputation: 159

As @geoB suggested, there was just this answer:

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

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

        $sells = $query->getResult();

Upvotes: 1

Related Questions