小编典典

在Doctrine2查询构建器中链接orX

sql

我必须在添加子句之后立即将OR表达式动态地添加到所返回的查询构建器中。我找不到任何合适的方式来做到这一点,我才刚刚开始学习Doctrine。getListQueryBuilder``where

我如何“链接”给定数量的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 */));

阅读 218

收藏
2021-03-10

共1个答案

小编典典

您可以检查此解决方案

$orX = $builder->expr()->orX();
foreach($ORs as $or) {
    $orX->add($or);
}
$builder->andWhere($orX);
2021-03-10