学说自定义查询生成器过滤器
问题描述:
这是(下面的代码不工作)情况:学说自定义查询生成器过滤器
if(is_array($filters))
{
$f = array();
foreach($filters as $filter)
{
$f[] = $qb->expr()->like("p.tags","'%" . $filter . "%'");
}
$qb->andWhere($qb->expr()->orx($f));
}
我需要自定义的通过/多个表达式ORX功能,但我不知道怎么样!
的ORX功能sistaxe是:
$ QB-> EXPR() - >和X($ COND1 [,$ condN])
固定例子(从教义的文档的提取):
$qb->add('select', $qb->expr()->select('u'))
->add('from', $qb->expr()->from('User', 'u'))
->add('where', $qb->expr()->orx(
$qb->expr()->eq('u.id', '?1'),
$qb->expr()->like('u.nickname', '?2')
))
请帮助!
答
(回答问题编辑转换为一个社区维基答案见What is the appropriate action when the answer to a question is added to the question itself?。)
的OP写道:
解决,解决方案:
if(is_array($filters)) { foreach($filters as $filter) { $temp[] = $qb->expr()->like("p.tags","'%" . $filter . "%'"); } $qb->andWhere(call_user_func_array(array($qb->expr(),'orx'), $temp)); }
答
Neater解决方案:)
if(is_array($filters))
{
$f = array();
$orCondition = $qb->expr()->orX();
foreach($filters as $filter)
{
$orCondition->add($qb->expr()->like("p.tags", "'%" . $filter . "%'"));
}
$qb->andWhere($orCondition);
}
或者,你可以这样做:
$orCondition->addMultiple(<conditions_array>);