如何在Doctrine 2中查询UPDATE SET field =!字段

时间:2014-08-24 18:27:09

标签: symfony doctrine-orm doctrine query-builder doctrine-query

我想查询" 更新类别c SET c.display =!c.display " 但我得到了例外。

我的代码:

$qb = $entityManager->createQueryBuilder();
$q = $qb->update('Category', 'c')
        ->set('c.display', '!c.display')
        ->getQuery();

$p = $q->execute();

我收到异常

Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'UPDATE Category c SET c.display = !c.display' in /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:41
Stack trace:
#0 /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(448): Doctrine\ORM\Query\QueryException::dqlError('UPDATE Category...')
#1 /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(2586): Doctrine\ORM\Query\Parser->syntaxError('Literal')
#2 /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(2774): Doctrine\ORM\Query\Parser->Literal()
#3 /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(2708): Doctrine\ORM\Query\Parser->ArithmeticPrimary()
#4 /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(2676): Doctrine\ORM\Query\Parser->ArithmeticFactor()
#5 /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(2650): Doctrine\ORM\Quer in /opt/web/sites/site/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php on line 52

1 个答案:

答案 0 :(得分:0)

如果您使用的数据库中布尔值存储为1或0,则可以尝试使用

$q = $qb->update('Category', 'c')
        ->set('c.display', '1 - c.display')
        ->getQuery();

请参阅https://stackoverflow.com/a/18458617/3574819(因此不允许我将其标记为重复,因为该答案未被接受)