Search code examples
phpmysqlsymfonydql

php query - Filter per date


I am trying to write query in my Symfony project where I have to select fields where I randomly pick dates to filter transaction data form db.

public function getFilter($startDate, $finalDate)
{
    $qb = new \DateTime();

    $initialDate = $this->getTransactionRepository()
        ->createQueryBuilder('d')
        ->select('sum(abs(d.donationAmount))')
        ->where($qb->expr()->between('s.date',':initialDate',':finalDate'))
        ->setParameter('initialDate', $startDate)
        ->setParameter('finalDate', $finalDate)
        ->getQuery()
        ->getSingleScalarResult();

    return $initialDate;
}

First error I ran into is

$qb->expr() as undefined method.


Solution

  • Why not simply use it like this:

            ->where('s.date BETWEEN :initialDate AND :finalDate')
    

    My knowledge on symfony is limited but DateTime is not really a Query-building class if you ask me.