Doctrine2 using setParameters

2019-04-24 04:13发布

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?

2条回答
狗以群分
2楼-- · 2019-04-24 04:35

thanks all for your efforts, i used it differently using the 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();
查看更多
我想做一个坏孩纸
3楼-- · 2019-04-24 04:43

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

查看更多
登录 后发表回答