WHERE a = 1 AND (b = 1 Or b = 2) AND (c = 1 OR c = 2)
我如何在Doctrine中做到这一点?
$q->where("a = 1");
$q->andWhere("b = 1")
$q->orWhere("b = 2")
$q->andWhere("c = 1")
$q->orWhere("d = 2")
这是错误的.应该是:
$q->where("a = 1");
$q->andWhere("b = 1")
$q->orWhere("b = 2")
$q->andWhere("c = 1")
$q->orWhere("d = 2")
但是我怎么才能做到呢?在Propel中是函数getNewCriterion,,在Doctrine中是...?
发布于 2012-02-01 19:44:42
$q->where("a = 1")
->andWhere("b = 1 OR b = 2")
->andWhere("c = 2 OR c = 2")
;
发布于 2015-12-29 02:45:52
下面是一个使用Doctrine 2.*和QueryBuilder
的复杂情况的例子
$qb->where('o.foo = 1')
->andWhere($qb->expr()->orX(
$qb->expr()->eq('o.bar', 1),
$qb->expr()->eq('o.bar', 2)
))
;
这些都是捷克学答案中提到的表达。
发布于 2018-02-21 21:02:57
这里缺少的一件事是:如果您希望将不同数量的元素放在一起,比如
WHERE [...] AND (field LIKE '%abc%' OR field LIKE '%def%')
如果你不想自己组装一个DQL字符串,你可以像这样使用上面提到的orX
:
$patterns = ['abc', 'def'];
$orStatements = $qb->expr()->orX();
foreach ($patterns as $pattern) {
$orStatements->add(
$qb->expr()->like('field', $qb->expr()->literal('%' . $pattern . '%'))
);
}
$qb->andWhere($orStatements);
https://stackoverflow.com/questions/9095113
复制相似问题