我有一个MySQL
查询,我想转换成一个由条令理解的查询。
SELECT name from users where project = 10 and (classification = 12
or classification is null)
所以我试图在教义上改变它,但它不起作用。
$query->AndWhere('users.classification = :classification');
$query->setParameter(':classification', $classification);
$query->orWhere('users.classification = :classification');
$query->setParameter(':classification', null);
我试过使用这个表达式,但我没有成功
最佳答案
要将括号中的多个where语句分组,可以使用orx
和andx
$queryBuilder->andWhere($queryBuilder->expr()->orX(
'users.classification = :classification',
'users.classification IS NULL'
))
->setParameter(':classification', $classification);
或者用教义表达:
$queryBuilder->andWhere($queryBuilder->expr()->orX(
$queryBuilder->expr()->eq('user.classification', ':classification'),
$queryBuilder->expr()->isNull('users.classification')
))
->setParameter(':classification', $classification);
条令文件:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#the-expr-class