BETWEEN condition select only one row Symfony2

Inputs:

:startPrice = 180
:targetPrice = 300

      

In the database I have a price column and you need to find all rows where the price column lies between 180 and 300.

Price values ​​in the database:

 price = 270
 price = 278

      

So as we can see that the query I wrote below should fetch rhese two rows, but I only get the first one (270).

Can someone explain to me why the BETWEEN condition only fetches one row from the DB? Here is the DB Query Builder:

$query = $result = $this->getEntityManager()
        ->createQueryBuilder()
        ->select('t')
        ->from('VputiTripBundle:Trip', 't');
             $query
               ->andWhere('t.price > :startPrice')
                ->andWhere('t.price < :targetPrice');
            $parameters = [
                'startPrice' => $startPrice,
                'targetPrice' => $targetPrice,
            ];
        $query->setParameters($parameters)
        ->setMaxResults(10)
        ->getQuery()
         ->getResult();

      

Query line:

'SELECT t FROM VputiTripBundle:Trip t WHERE t.price > :startPrice AND t.price < :targetPrice'

      

+3


source to share


1 answer


Try it like this:



$queryBuilder = $this->getEntityManager()->createQueryBuilder();

$query = $queryBuilder
        ->select('t')
        ->from('VputiTripBundle:Trip', 't')
        ->where($queryBuilder->expr()->between('t.price', ':startPrice', ':targetPrice'))
        ->setParameters([
            'startPrice' => $startPrice,
            'targetPrice' => $targetPrice
        ])
        ->getQuery();

        $query->setMaxResults(10);

        return $query->getResult();

      

+1


source







All Articles