I have to dynamically add OR
expressions to the query builder returned by getListQueryBuilder
, right after adding a where
clause. I can't find any suitable way of doing this, i'm just started learning Doctrine.
我必须在添加where子句后立即动态地将OR表达式添加到getListQueryBuilder返回的查询构建器中。我找不到任何合适的方法,我刚刚开始学习Doctrine。
How can i "chain" a given number of orX
and add them to my builder?
如何“链接”给定数量的orX并将它们添加到我的构建器中?
public function getListQueryBuilder($ownerId)
{
$qb = $this->createQueryBuilder('t');
return $qb
->where($qb->expr()->eq('t.user', ':user'))
->setParameter('user', $ownerId);
}
$builder = getListQueryBuilder(4);
// $ORs is a dynamically builded array, here is just an example
$ORs = array();
$ORs[] = $builder->expr()->like("t.name", 'my name');
$ORs[] = $builder->expr()->like("t.description", 'desc');
// Adding ORs to the builder
$builder->andWhere($builder->expr()->orX(/* here */));
2 个解决方案
#1
21
You can check this solution:
您可以查看此解决方案:
$orX = $builder->expr()->orX();
foreach($ORs as $or) {
$orX->add($or);
}
$builder->andWhere($orX);
#2
8
I stumbled on the same problem and tried :
我偶然发现了同样的问题并尝试过:
$builder->andWhere($builder->expr()->orX($ORs));
but it does not work since orX calls "return new Expr\Orx(func_get_args());" internally and you end up with something like array(array(or1, or2))
但它不起作用,因为orX调用“返回新的Expr \ Orx(func_get_args());”在内部,你最终得到像array(array(or1,or2))之类的东西
having looked at the API however i figured you can do this:
看了API然而我认为你可以做到这一点:
$builder->andWhere($builder->expr()->orX()->addMultiple($ORs));
OR do use $ORs table at all but issue:
或者根本使用$ ORs表,但问题是:
$orx = $builder->expr()->orX();
$orx->add($builder->expr()->like("t.name", 'my name'));
$orx->add($builder->expr()->like("t.description", 'desc'));
$builder->andWhere($orx);
#1
21
You can check this solution:
您可以查看此解决方案:
$orX = $builder->expr()->orX();
foreach($ORs as $or) {
$orX->add($or);
}
$builder->andWhere($orX);
#2
8
I stumbled on the same problem and tried :
我偶然发现了同样的问题并尝试过:
$builder->andWhere($builder->expr()->orX($ORs));
but it does not work since orX calls "return new Expr\Orx(func_get_args());" internally and you end up with something like array(array(or1, or2))
但它不起作用,因为orX调用“返回新的Expr \ Orx(func_get_args());”在内部,你最终得到像array(array(or1,or2))之类的东西
having looked at the API however i figured you can do this:
看了API然而我认为你可以做到这一点:
$builder->andWhere($builder->expr()->orX()->addMultiple($ORs));
OR do use $ORs table at all but issue:
或者根本使用$ ORs表,但问题是:
$orx = $builder->expr()->orX();
$orx->add($builder->expr()->like("t.name", 'my name'));
$orx->add($builder->expr()->like("t.description", 'desc'));
$builder->andWhere($orx);