Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 using setParameters

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?

like image 771
Confidence Avatar asked Apr 03 '12 07:04

Confidence


2 Answers

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

like image 144
Jakub Zalas Avatar answered Oct 20 '22 06:10

Jakub Zalas


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();
like image 32
Confidence Avatar answered Oct 20 '22 06:10

Confidence