Doctrine自定义查询生成器过滤器

时间:2011-04-27 13:37:43

标签: function parameters doctrine-orm

这种情况(下面的代码不起作用):

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() - > andx($ cond1 [,$ condN])

修复示例(从doctrine doc中提取):

$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')
))

请帮助!!

2 个答案:

答案 0 :(得分:0)

(在问题编辑中回答。转换为社区维基答案。请参阅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));
 }

答案 1 :(得分:0)

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>);