WHERE a = 1 AND (b = 1 Or b = 2) AND (c = 1 OR c = 2)
Run Code Online (Sandbox Code Playgroud)
我怎么能在Doctrine中做到这一点?
$q->where("a = 1");
$q->andWhere("b = 1")
$q->orWhere("b = 2")
$q->andWhere("c = 1")
$q->orWhere("d = 2")
Run Code Online (Sandbox Code Playgroud)
这不正确......应该是:
$q->where("a = 1");
$q->andWhere("b = 1")
$q->orWhere("b = 2")
$q->andWhere("c = 1")
$q->orWhere("d = 2")
Run Code Online (Sandbox Code Playgroud)
但我怎么能做到呢?在Propel中是函数getNewCriterion,在Doctrine中......?
Mae*_*lyn 102
$q->where("a = 1")
->andWhere("b = 1 OR b = 2")
->andWhere("c = 2 OR c = 2")
;
Run Code Online (Sandbox Code Playgroud)
Ser*_*nin 67
这是一个例子,适用于那些条件更复杂且使用Doctrine 2.*的人QueryBuilder:
$qb->where('o.foo = 1')
->andWhere($qb->expr()->orX(
$qb->expr()->eq('o.bar', 1),
$qb->expr()->eq('o.bar', 2)
))
;
Run Code Online (Sandbox Code Playgroud)
这些是Czechnology答案中提到的表达.
Cze*_*ogy 12
为什么不呢
$q->where("a = 1");
$q->andWhere("b = 1 OR b = 2");
$q->andWhere("c = 1 OR d = 2");
Run Code Online (Sandbox Code Playgroud)
编辑:您也可以使用Expr类(Doctrine2).
这里缺少一件事:如果您想将不同数量的元素放在一起,例如
WHERE [...] AND (field LIKE '%abc%' OR field LIKE '%def%')
Run Code Online (Sandbox Code Playgroud)
不想自己组装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);
Run Code Online (Sandbox Code Playgroud)