php 将条件数组传递给学说 expr()->orx() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11704447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass array of conditions to doctrine expr()->orx() method
提问by Bartosz Rychlicki
I need to construct DQL with a QueryBuilderlike this
我需要用QueryBuilder这样的方式构造 DQL
[QUERY]... AND WHERE e.type = x OR e.type = Y OR e.type = N [...]
I have types in array How can I pass this array to my query builder?
我在数组中有类型如何将此数组传递给我的查询构建器?
$qb->andWhere($qb->expr()->orx(CONDITIONS));
List of types will be dynamic, calling $qb->andWhereon each foreach types loop will make only more AND WHERE's no more ORs.
Can I store multiply orxexpressions and then add it to andWhere? Any idea how to solve this, probably, common problem?
类型列表将是动态的,调用$qb->andWhere每个 foreach 类型循环只会产生更多 AND WHERE 没有更多的 OR。
我可以存储乘法orx表达式然后将其添加到andWhere吗?知道如何解决这个可能是常见的问题吗?
采纳答案by Bartosz Rychlicki
I knew that tommarow gonna be a better day. The solution is simple. Your can make array of OR expressions like so
我知道明天会更好。解决方法很简单。您可以像这样制作 OR 表达式数组
$ors[] = $qb->expr()->orx('e.type = '.$qb->expr()->literal($value));
And then just add it to andWhere()/Where() method of the query builder via join method like so:
然后只需通过 join 方法将其添加到查询构建器的 andWhere()/Where() 方法中,如下所示:
$qb->andWhere(join(' OR ', $ors));
回答by DEY
I hope so, then I found this :
我希望如此,然后我发现了这个:
$conditions = array('e.type = x', 'e.type = Y', 'e.type = N');
$orX = $qb->expr()->orX();
foreach ($conditions as $condition) {
$orX->add($condition);
}
$qb->add('where', $orX);
Using @meze suggestion, you can simplify the code and replace the foreachstatement with:
使用@meze 建议,您可以简化代码并将foreach语句替换为:
$orX->addMultiple($conditions);
回答by Wilt
@DEY his answer can be simplified. No need for the foreach, this also works:
@DEY 他的回答可以简化。不需要 foreach,这也有效:
$conditions = array('e.type = x', 'e.type = Y', 'e.type = N');
$orX = $qb->expr()->orX();
$orX->addMultiple($conditions);
$qb->where($orX);
回答by Karol Gasienica
You can also use ...in php like:
您还可以...在 php 中使用,例如:
$conditions = array('e.type = x', 'e.type = Y', 'e.type = N');
$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->orX(...$conditions));
回答by Jens
You can also use the call_user_func_arrayfunction like this.
您也可以像这样使用call_user_func_array函数。
It lets you call a method passing an array's items as parameters.
它允许您调用传递数组项作为参数的方法。
For example:
例如:
$conditions = array('e.type = x', 'e.type = Y', 'e.type = N');
$expr = $qb->expr();
call_user_func_array(array($expr, 'orX'), $conditions);

