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?
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With