0

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

xihoj6
  • 7
  • 4
  • 1
    Try looking how parameters are being sent by printing `$query->getQuery()->getParameters()`. You could check [these](https://stackoverflow.com/a/12431539/4820276) solutions and relative comments too – Agnohendrix Oct 01 '20 at 10:59
  • $query->getQuery()->getParameters() returns [ ] ? @Agnohendrix – xihoj6 Oct 01 '20 at 11:02
  • yes, it returns an Array of Objects, if you `var_dump($query->getQuery()->getParameters())` you'll see every parameter you set in your query. – Agnohendrix Oct 01 '20 at 11:08
  • 1
    your `getInvoices()` looks fine. Check the controller, probably `$this->data['startDate']`is null. – simon.ro Oct 01 '20 at 21:40
  • Please, post actual controller code, in your sample code `$data` is a local variable, but you are passing an instance variable to the search function: `$this->data`, and you are not reading actual request data anywhere. – msg Oct 06 '20 at 15:57

0 Answers0