Symfony2 - 参数号无效

时间:2016-11-18 15:00:00

标签: symfony doctrine-orm

我有这个错误:

  

参数号无效:绑定变量数不匹配   令牌数量

当我想在我的存储库中获取 getAllOrders()时:

public function allOrdersQB()
{
    return $this->createQueryBuilder('o')
        ->andWhere('o.state != :canceled')
        ->andWhere('o.state != :receipt_complete')
        ->setParameters(array(
        'receipt_complete' => 'receipt_complete',
        'canceled' => 'canceled',
    ));
}

public function getAllOrders()
{
    return $this->allOrdersQB()
        ->andWhere('o.stateCorp = :stateCorp')
        ->setParameters(array(
            'stateCorp' => 0,
        ))
        ->getQuery()->getResult();
}

我不明白,因为我已经设置了所有参数。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

setParameters方法会重置之前的所有参数,因此您可以使用简单的setParameter调用(请参阅源代码here),例如:

public function allOrdersQB()
{
    return $this->createQueryBuilder('o')
        ->andWhere('o.state != :canceled')
        ->andWhere('o.state != :receipt_complete')
        ->setParameter('receipt_complete', 'receipt_complete')
        ->setParameter('canceled', 'canceled')
}

public function getAllOrders()
{
    return $this->allOrdersQB()
        ->andWhere('o.stateCorp = :stateCorp')
        ->setParameter('stateCorp', 0)
        ->getQuery()->getResult();
}

希望这个帮助