symfonydoctrinesymfony-2.8

Too many parameters: the query defines 0 parameters and you bound 1 error


I'm new to Symfony, and I got an error. I was looking for solutions on stackoverflow but none seems to fit my problem. Error: "Too many parameters: the query defines 0 parameters and you bound 1" while running a query :

class Report extends BaseEntityManager
{
    use ContainerAwareTrait;
    
    public function getPager(array $criteria, $page = 1, $limit = 5, array $sort = array()){
        $parameters = array();
        $query = $this->getRepository()
                ->createQueryBuilder('r')
                ->select('r, g')
                ->leftJoin('r.media', 'g')
                ->leftJoin('r.category', 'c')
                ->orderBy('r.id', 'ASC');
        
        if (isset($criteria['context'])){
            $query->andWhere('c.context = :contextId');
            $parameters['contextId'] = $criteria['context'];
            
        }
        
        if(isset($criteria['category'])){
            $query->andWhere('r.category = :categoryId');
            $parameters['categoryId'] = $criteria['category'];
        }
        
        if(isset($criteria['author'])){
            $query->andWhere('r.user = :author');
            $parameters['author'] = $criteria['author'];
        }
        
        if(isset($criteria['search'])){
            $query->andWhere('lower(r.title) LIKE lower(:search) OR lower(r.description) LIKE lower(:search) OR lower(r.location) LIKE lower(:search)');
            $parameters['search'] = '%'.$criteria['search'].'%';
            
        }
        
        if(isset($criteria['status'])){
//            $query->andWhere('r.status = :status');
//            $parameters['status'] = $criteria['status'];
            $s0= "'".$criteria['status'][0]."'";
            $s1= "'".$criteria['status'][1]."'";
            $s2= "'".$criteria['status'][2]."'";

            $query->andWhere('r.status = '.$s0.' OR r.status = '.$s1.' OR r.status = '.$s2);
            $parameters['status'] = $criteria['status'];

        }
   
  

        $query->setParameters($parameters);
        $adapter = new DoctrineORMAdapter($query);
        $pagerfanta = new Pagerfanta($adapter);
        
        if($limit == -1){
            $pagerfanta->setMaxPerPage($pagerfanta->getNbResults());
        }
        else{
            $pagerfanta->setMaxPerPage($limit)
                ->setCurrentPage($page);
        }
        return $pagerfanta;
        
    }

}

nevermaind text becouse: It looks like your post is mostly code; please add some more details. nevermaind text becouse: It looks like your post is mostly code; please add some more details.


Solution

  • There is no named placeholder as :status but in $parameters you are passing status value from criteria

    $parameters['status'] = $criteria['status'];
    

    Moreover you are directly passing status values in query which is not a good approach instead use named placeholders for status values

    You can also use IN() clause for status values

    if(isset($criteria['status'])){
        $query->andWhere('r.status IN(:status)');
        $parameters['status'] = $criteria['status'];
    }