Doctrine2使用setParameters

Con*_*nce 8 sql symfony doctrine-orm

当我似乎在我的查询中使用参数时,我收到一个错误

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

这是我的代码

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;
}
Run Code Online (Sandbox Code Playgroud)

如何正确配置参数数组?

Jak*_*las 24

您没有在查询中包含参数.

$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);
Run Code Online (Sandbox Code Playgroud)

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

  • 我实际上说这是件好事.如果你传递的参数多于占位符,那么你做错了什么,最好知道它. (2认同)