nowiko
nowiko

Reputation: 2557

BETWEEN condition select only single row Symfony2

Inputs:

:startPrice = 180
:targetPrice = 300

In database I have a price column and need to find all rows where price column lay beetwen 180 and 300.

Price values in DB:

 price = 270
 price = 278

So how we can see the query what I wrote below must fetch rhese two rows, but I getting only first(270).

Can somebody explain me why BETWEEN condition fetch only one row from DB? Here is 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 string:

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

Upvotes: 3

Views: 373

Answers (1)

xurshid29
xurshid29

Reputation: 4210

Try 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();

Upvotes: 1

Related Questions