如何将 andWhere 和 orWhere 与 Doctrines Criteria 结合起来

Pri*_*muS 4 php doctrine criteria symfony

我想使用 Doctrines 创建一个相当复杂且灵活的查询Criteria。我有一个andWhere包含在每个查询中的参数,但是随后(取决于输入)我想添加一些可选参数orWhere

$criteria = Criteria::create();

/* Add Criteria for last 55 Days (every query) */
$criteria->where(Criteria::expr()->gte('gameCreation', $long));

/* Gather optional Params */
$orxList = array();

/* Optional Criteria */
if (!empty($champions)) {
    foreach ($champions as $c) {
        $orxList[] = Criteria::expr()->eq('champion',$c);
    }
}

...
$criteria->andWhere(Criteria::expr()->orX($orxList));
Run Code Online (Sandbox Code Playgroud)

这会导致异常:

没有为 CompositeExpression 提供任何表达式。

我如何将这样的标准与初始where条款结合起来?

Fab*_*ian 5

我遇到了类似的问题并这样解决:

use Doctrine\Common\Collections\Expr\CompositeExpression;
...

/* Gather optional Params */
$orxList = array();

/* Optional Criteria */
if (!empty($champions)) {
    foreach ($champions as $c) {
        $orxList[] = Criteria::expr()->eq('champion', $c);
    }
}

...
$criteria->andWhere(new CompositeExpression(CompositeExpression::TYPE_OR, $orxList));
Run Code Online (Sandbox Code Playgroud)

正如您所看到的, 的构造函数CompositeExpression接受表达式数组,这会生成正确的查询。