Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 790
nowiko Avatar asked Apr 16 '26 08:04

nowiko


1 Answers

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();
like image 57
xurshid29 Avatar answered Apr 17 '26 21:04

xurshid29



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!