Doctrine2 using setParameters

when i seem to use parameters in my request i get an error

Invalid parameter number: the number of related variables does not match the 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 to properly configure an array of parameters?

+6
source share
2 answers

You did not specify your parameters in the request.

 $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

+21
source

Thanks to everyone for your efforts, I used it differently with 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
source

Source: https://habr.com/ru/post/912295/


All Articles