Doctrine2 using setParameters
46,279
Solution 1
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
Solution 2
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();
Author by
Confidence
Passionate for the web, php, python, docker, clean code And Vanilla Ice scream with chocolate sprinkles.
Updated on July 09, 2022Comments
-
Confidence almost 2 years
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?
-
Cerad about 12 yearsYep. One of the more annoying things about PDO's prepared statements is that having extra parameters will trigger an error.
-
Jakub Zalas about 12 yearsI'd actually say it's a good thing. If you're passing more parameters than placeholders, you did something wrong and it's better to be informed about it.
-
Jakub Zalas about 12 yearsYou didn't solve it differently but just used different way of creating queries. It worked as you provided parameters to be bound (just like I suggested).