Doctrine 2如何一次增加多行的列?

时间:2015-12-03 12:06:23

标签: doctrine-orm doctrine

我正在寻找一种方法来一次增加多行的值而不需要循环,是否可以在学说中使用? 这是简单的SQL中的查询:

$sql = "UPDATE table set compteur = compteur + 1 where id in ('1','2','3') ";

使用doctrine更新许多行(不递增),我有这个:

$qb = $this->getEntityManager()->createQueryBuilder();
                    $query = $qb->update('Application\Entity\Table', 'l')
                            ->set('l.compteur', $qb->expr()->literal('8'))
                            ->where("l.id in ('$ids')")
                            ->getQuery();
                    $retour = $query->execute();

感谢您的任何想法!!

2 个答案:

答案 0 :(得分:0)

使用DQL:

$this->getEntityManager()->createQuery('
    UPDATE Application\Entity\Table t
    SET t.compteur = t.compteur + 1
')
->execute();

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/batch-processing.html#dql-update

答案 1 :(得分:0)

您也可以使用querybuilder做到这一点,

$qb->set('l.compteur', $qb->expr()->sum('l.compteur', 1));