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

两盒软妹~` 提交于 2019-11-30 01:24:17
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 foreach statement with:

$orX->addMultiple($conditions);

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!