Datetime range won’t select specific date

I have a method to return results from db from startDate to endDate passing in like parameters in postman.

Everything seems to be working fine but it returns all results from database.

I think that problem could be with parameters..

public function getInvoices($fromDate = null, $toDate = null)
{
    $query = $this->entityManager->getRepository(Invoice::class)
        ->createQueryBuilder('ur')
        ->select('ur')
        ->orderBy('ur.invoiceDate', 'ASC');

    if($fromDate) {
        $query
            ->andWhere('t.invoiceDate >= :startDate')
            ->setParameter('startDate', new DateTime($fromDate));
    }

    if($toDate) {
        $query
            ->andWhere('t.invoiceDate <= :endDate')
            ->setParameter('endDate', new DateTime($toDate));
    }

    return $query->getQuery()->getResult();
}

And I am calling this method in my controller like:

$data = [];

$responseData = $this->invoiceService->getInvoices(
        @$this->data['startDate'],
        @$this->data['endDate']
    );

I pass this two parameters via postman and it returns all results:

Here is an image: here

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *