将mysql查询转换为symfony原则

bmp9r5qi  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(282)

我有这个 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);

我试过用这个词 $query->expr() 我没有成功

qmelpv7a

qmelpv7a1#

要将多个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-表达式类

相关问题