pass array of conditions to doctrine expr()->orx() method

后端 未结 5 1713
忘掉有多难
忘掉有多难 2020-12-25 10:56

I need to construct DQL with a QueryBuilder like this

[QUERY]... AND WHERE e.type = x OR e.type = Y OR e.type = N [...]

I hav

相关标签:
5条回答
  • 2020-12-25 11:06

    You can also use the call_user_func_array function like this.

    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);
    
    0 讨论(0)
  • 2020-12-25 11:07

    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 foreach statement with:

    $orX->addMultiple($conditions);
    
    0 讨论(0)
  • 2020-12-25 11:07

    @DEY his answer can be simplified. No need for the foreach, this also works:

    $conditions = array('e.type = x', 'e.type = Y', 'e.type = N');
    
    $orX = $qb->expr()->orX();
    $orX->addMultiple($conditions);
    
    $qb->where($orX);
    
    0 讨论(0)
  • 2020-12-25 11:18

    I knew that tommarow gonna be a better day. The solution is simple. Your can make array of OR expressions like so

    $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:

    $qb->andWhere(join(' OR ', $ors));
    
    0 讨论(0)
  • 2020-12-25 11:31

    You can also use ... in php like:

    $conditions = array('e.type = x', 'e.type = Y', 'e.type = N');
    $criteria = Criteria::create();
    $criteria->andWhere(Criteria::expr()->orX(...$conditions));
    
    0 讨论(0)
提交回复
热议问题