所需的查询包括一个 where 语句,该语句由基本帐户要求以及任何可能的用户选择组成,其数据成对。下面的代码不适用于多个选择,即 else {} 不正确。有没有办法有条件地在 andWhere 中的 orX 中构建 andX?
$queryBuilder
...
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('t.code', $queryBuilder->createPositionalParameter($this->code)),
$queryBuilder->expr()->neq('det.completed', '1')
)
);
if (count($selected) == 0) {
// no results returned
}
else if (count($selected) == 1) {
$queryBuilder->andWhere('det.co_id=' . $queryBuilder->createPositionalParameter($co_array[0]) . ' AND det.ct_id=' . $queryBuilder->createPositionalParameter($ct_array[0]));
}
else {
$nestedWhere = $this->db->createQueryBuilder();
for ($i=0; $i < count($selected); $i++) {
$nestedWhere->expr()->orX(
$nestedWhere->expr()->andX(
$nestedWhere->expr()->eq('det.co_id', $nestedWhere->createPositionalParameter($co_array[$i])),
$nestedWhere->expr()->eq('det.ct_id', $nestedWhere->createPositionalParameter($ct_array[$i]))
)
);
}
$queryBuilder->andWhere($nestedWhere->expr()); // need dynamic list of orX here
}
我找到了CompositeExpression类,我的问题似乎是它的确切目的。我的解决方案的代码和指向 Doctrine 文档的链接如下。
http://www.doctrine-project.org/api/dbal/2.3/class-Doctrine.DBAL.Query.Expression.CompositeExpression.html
// replacing the else {} in thequestion above
else {
$orX = new CompositeExpression(CompositeExpression::TYPE_OR);
for ($i=0; $i < count($selected); $i++) {
$andX = new CompositeExpression(CompositeExpression::TYPE_AND);
$andX->add('det.co_id = :co' . (string)$i);
$andX->add('det.ct_id = :ct' . (string)$i);
$orX->add($andX);
$queryBuilder
->setParameter("co{$i}", $co_array[$i])
->setParameter("ct{$i}", $ct_array[$i]);
}
$queryBuilder->andWhere($orX);
}