Too few parameters: the query defines n parameters but you only bound n-n #6476

Closed
opened 2026-01-22 15:33:50 +01:00 by admin · 2 comments
Owner

Originally created by @faso-dev on GitHub (May 28, 2020).

Sorry if this is not the right place to expose the problem.

When we use the queryBuilder and we call setParameter first then setParameters in second, it overwrites the data defined with the setParameter, from my point of view I think it's inconsistent, because normally it should merge them or add them to the parameter table if they do not exist.
The above example reproduces the error:

    $builder = $this->queryBuilder()
            ->select('p', 'category', 'codePromo')
            ->leftJoin('p.category', 'category')
            ->leftJoin('p.codePromo', 'codePromo')
            ->leftJoin('p.images', 'images')
        ;
        if ( null !== $category){
            if (is_array($category)){
                $builder
                    ->andWhere('p.category IN (:categories)')
                    ->setParameter('categories', $category);
            }elseif ($category instanceof Category){
                $builder->andWhere('p.category = :category')
                    ->setParameter('category', $category);
            }
        }
      //The olds parameters values are lost after setParameters
      if ($search->getMinPrice() && $search->getMaxPrice()){
            $builder->andWhere('p.price BETWEEN :min AND :max')
                    ->setParameters([
                        'min' => $search->getMinPrice(),
                        'max' => $search->getMaxPrice(),
                    ]);
        }
      if ($search->getSearch()){
            $builder->andWhere('p.title LIKE :search')
                ->setParameter('search', '%'.$search->getSearch().'%');
        }
       return $builder->getQuery()->getResult();
Originally created by @faso-dev on GitHub (May 28, 2020). Sorry if this is not the right place to expose the problem. When we use the queryBuilder and we call setParameter first then setParameters in second, it overwrites the data defined with the setParameter, from my point of view I think it's inconsistent, because normally it should merge them or add them to the parameter table if they do not exist. The above example reproduces the error: ```php $builder = $this->queryBuilder() ->select('p', 'category', 'codePromo') ->leftJoin('p.category', 'category') ->leftJoin('p.codePromo', 'codePromo') ->leftJoin('p.images', 'images') ; if ( null !== $category){ if (is_array($category)){ $builder ->andWhere('p.category IN (:categories)') ->setParameter('categories', $category); }elseif ($category instanceof Category){ $builder->andWhere('p.category = :category') ->setParameter('category', $category); } } //The olds parameters values are lost after setParameters if ($search->getMinPrice() && $search->getMaxPrice()){ $builder->andWhere('p.price BETWEEN :min AND :max') ->setParameters([ 'min' => $search->getMinPrice(), 'max' => $search->getMaxPrice(), ]); } if ($search->getSearch()){ $builder->andWhere('p.title LIKE :search') ->setParameter('search', '%'.$search->getSearch().'%'); } return $builder->getQuery()->getResult(); ```
admin closed this issue 2026-01-22 15:33:51 +01:00
Author
Owner

@Xesau commented on GitHub (Jul 8, 2020):

The API documentation specifically states that this is intended behaviour. I think in your case it's best to simply rewrite your code

            $builder->andWhere('p.price BETWEEN :min AND :max')
                ->setParameter('min', $search->getMinPrice())
                ->setParameter('max', $search->getMaxPrice());
@Xesau commented on GitHub (Jul 8, 2020): The API documentation specifically states that this is intended behaviour. I think in your case it's best to simply rewrite your code ```php $builder->andWhere('p.price BETWEEN :min AND :max') ->setParameter('min', $search->getMinPrice()) ->setParameter('max', $search->getMaxPrice()); ```
Author
Owner

@faso-dev commented on GitHub (Jul 8, 2020):

The API documentation specifically states that this is intended behaviour. I think in your case it's best to simply rewrite your code

            $builder->andWhere('p.price BETWEEN :min AND :max')
                ->setParameter('min', $search->getMinPrice())
                ->setParameter('max', $search->getMaxPrice());

Yes I noticed. This is the solution that I finally opted for

@faso-dev commented on GitHub (Jul 8, 2020): > The API documentation specifically states that this is intended behaviour. I think in your case it's best to simply rewrite your code > > ``` > $builder->andWhere('p.price BETWEEN :min AND :max') > ->setParameter('min', $search->getMinPrice()) > ->setParameter('max', $search->getMaxPrice()); > ``` Yes I noticed. This is the solution that I finally opted for
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6476