title: Symfony and Doctrine DQL query builder: how to use multiple setParameters inside if condition checks?
body: <p>Using Symfony and Doctrine with the DQL query builder, I need to optionally add some WHERE conditions with parameters, with some if condition checks.</p>
  
  <p>Pseudo code example:</p>
  
  <pre><code>$qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
  $qb = $qb
      -&gt;select('SOMETHING')
      -&gt;from('SOMEWHERE')
      -&gt;where('SOME CONDITIONS');
  
  
  if ( SOME CHECK ) {
      $qb
          -&gt;andWhere('field1 = :field1')
          -&gt;andWhere('field2 = :field2')
          -&gt;setParameters([
              'field1' =&gt; $myFieldValue1,
              'field2' =&gt; $myFieldValue2,
          ]);
  } else {
      $qb
          -&gt;andWhere('field1 = :field1')
          -&gt;setParameters([
              'field1' =&gt; $myOtherFieldValue1,
          ]);
  }
  </code></pre>
  
  <p>Getting errors like:</p>
  
  <blockquote>
    <p>Invalid parameter number: number of bound variables does not match number of tokens</p>
    
    <p>Too few parameters: the query defines X parameters but you only bound Y</p>
    
    <p>Too many parameters: the query defines X parameters and you bound Y</p>
  </blockquote>