Comparing current date with stored dated on doctrine querybuilder

Viewed 26

I need to make a query to get the row where the active field is true or the current date is between two stored dates Itring with

 $qb->andWhere('p.active = true')')
                ->orWhere($qb->expr()->andX(
                    $qb->expr()->lt('p.activationDate', 'now()'),
                    $qb->expr()->eq('p.deactivationDate', 'now()')
                ))
            ->setMaxResults(1);

But I get :

[Syntax Error] line 0, col 145: Error: Expected known function, got 'now'
1 Answers

the were can be like this:

->andWhere("CURRENT_TIMESTAMP() BETWEEN p.activationDate and p.deactivationDate")

like this:

$repo = $this->em->getRepository(YourRepo::class);
$result = $repo->createQueryBuilder("p")
    ->andWhere("CURRENT_TIMESTAMP() BETWEEN p.activationDate and p.deactivationDate")
    ->getQuery()
    ->getResult();
Related