Update query with LIMIT ( setMaxResults ) in doctrine2 using createQueryBuilder not working
Asked Answered
K

2

7

I have the following code

$qb = $this->createQueryBuilder('cs')
        ->update()
        ->set('cs.is_active', 1)
        ->where('cs.reward_coupon = :reward_coupon')
        ->setMaxResults($limit)
        ->setParameter('reward_coupon', $rewardCoupon);
$qb->getQuery()->execute(); 

This doesn’t apply the LIMIT in the resultant query.

Kelbee answered 1/9, 2014 at 12:59 Comment(0)
D
3

setMaxResult() has to be your last Doctrine statement in order to properly works

example :

    $qb = $this->createQueryBuilder('cs')
    ->update()
    ->set('cs.is_active', 1)
    ->where('cs.reward_coupon = :reward_coupon')
    ->setParameter('reward_coupon', $rewardCoupon)
    ->setMaxResults($limit);


     return $qb->getQuery()->execute(); 
Danicadanice answered 1/9, 2014 at 13:40 Comment(0)
D
-1

I think that this may help

$limit=50;
$i=0;
$qb = $this->createQueryBuilder('cs')
->update()
->set('cs.is_active', 1)
->where('cs.reward_coupon = :reward_coupon')
->setParameter('reward_coupon', $rewardCoupon)
->setFirstResult($i)
->setMaxResults($limit);
$qb->getQuery()->execute(); 
Ditchwater answered 6/9, 2014 at 8:55 Comment(1)
You cannot set a starting index for LIMIT when used in an UPDATE statement.Silvey

© 2022 - 2024 — McMap. All rights reserved.