将条件数组传递给教义 expr()->orx() 方法

2022-01-16 00:00:00 oop php doctrine doctrine-orm dql

我需要像这样使用 QueryBuilder 构造 DQL

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 have types in array How can I pass this array to my query builder?

$qb->andWhere($qb->expr()->orx(CONDITIONS));

类型列表将是动态的,在每个 foreach 类型循环上调用 $qb->andWhere 只会产生更多 AND WHERE 不再有 OR.
我可以存储乘法 orx 表达式,然后将其添加到 andWhere 中吗?知道如何解决这个可能是常见问题吗?

List of types will be dynamic, calling $qb->andWhere on each foreach types loop will make only more AND WHERE's no more ORs.
Can I store multiply orx expressions and then add it to andWhere? Any idea how to solve this, probably, common problem?

推荐答案

我知道明天会更好.解决方案很简单.您可以像这样制作 OR 表达式数组

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

然后通过 join 方法将其添加到查询构建器的 andWhere()/Where() 方法中,如下所示:

And then just add it to andWhere()/Where() method of the query builder via join method like so:

$qb->andWhere(join(' OR ', $ors));

相关文章