sql – 使用setParameters的Doctrine2

前端之家收集整理的这篇文章主要介绍了sql – 使用setParameters的Doctrine2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我似乎在我的查询中使用参数时,我收到一个错误

Invalid parameter number: number of bound variables does not match number of tokens

这是我的代码

public function GetGeneralRatingWithUserRights($user,$thread_array)
{
    $parameters = array(
        'thread' => $thread_array['thread'],'type' => '%'.$thread_array['type'].'%'
    );

    $dql = 'SELECT p.type,AVG(p.value) 
        FROM TrackerMembersBundle:Rating p 
        GROUP BY p.thread,p.type';

    $query = $this->em->createQuery($dql)
        ->setParameters($parameters);

    $ratings = $query->execute();

    return $ratings;
}

如何正确配置参数数组?

解决方法

您没有在查询中包含参数.
$parameters = array(
    'thread' => $thread_array['thread'],'type' => '%'.$thread_array['type'].'%'
);

$dql = 'SELECT p.type,AVG(p.value) 
    FROM TrackerMembersBundle:Rating p 
    WHERE p.thread=:thread 
    AND type LIKE :type 
    GROUP BY p.thread,p.type';

$query = $this->em->createQuery($dql)
    ->setParameters($parameters);

请参阅文档中的示例:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples

猜你在找的MsSQL相关文章