SQL Doctrine2 使用 setParameters

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9988866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:08:24  来源:igfitidea点击:

Doctrine2 using setParameters

sqlsymfonydoctrine-orm

提问by Confidence

when I seem to use parameters in my query, I get an error

当我似乎在查询中使用参数时,出现错误

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

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

here is my code

这是我的代码

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;
}

How do I configure the parameters array properly?

如何正确配置参数数组?

回答by Jakub Zalas

You didn't include your parameters in the query.

您没有在查询中包含您的参数。

$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);

See examples in the documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples

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

回答by Confidence

thanks all for your efforts, i used it differently using the querybuilder

感谢大家的努力,我使用 querybuilder 以不同的方式使用它

        $parameters = array(
        'thread' => $thread_array['thread']
        ,'type' => $thread_array['type']
    );


    $qb = $this->em->createQueryBuilder();
    $query = $qb
        ->from('TrackerMembersBundle:Rating','rating')
        ->select(' rating.type,
        COUNT(rating.value) AS ratingcount ,
        AVG(rating.value) AS ratingaverage ')
        ->where(
        $qb->expr()->orx(
            $qb->expr()->eq('rating.thread', ':thread'),
            $qb->expr()->like('rating.type', ':type')
        )

    )
        ->groupBy('rating.thread,rating.type')
        ->setParameters($parameters)
        ->getQuery();