title: How to use andWhere and orWhere together in a doctrine query builder
body: <p>I've tried to find the answer to this elsewhere but cannot, but it seems that my query is working but not returning the results expected.</p>
  
  <p>I need to run a check to find out if a date and set of times overlap with an existing record in a table. So, I take the date and times and do a check to see if the date matches and if it does, if the times chosen fall between the times in the table.</p>
  
  <p>This is an example entry in the table:</p>
  
  <pre><code>| id | booking_date | start_time | end_time |
  ---------------------------------------------
  |  1 | 2017-11-26   |  11:00     |  13:00   |
  ---------------------------------------------
  </code></pre>
  
  <p>I enter the date 2017-11-27 in the form, with the times 11:00 - 13:00. Technically this DOESN'T overlap since the date in the table is the 26th.</p>
  
  <p>This is my Doctrine query:</p>
  
  <pre><code>$qb = $this-&gt;getEntityManager()-&gt;createQueryBuilder();
  $qb-&gt;select('COUNT(b.id)')
      -&gt;from('AppBundle:Booking', 'b')
      -&gt;where('b.bookingDate = :date')
      -&gt;andWhere('b.startTime BETWEEN :start AND :end')
      -&gt;orWhere('b.startTime = :start')
      -&gt;orWhere('b.endTime BETWEEN :start AND :end')
          -&gt;setParameter('date', $date)
          -&gt;setParameter('start', $start)
          -&gt;setParameter('end', $end)
  ;
  
  return $qb-&gt;getQuery()-&gt;getSingleScalarResult();
  </code></pre>
  
  <p>But for some reason this always returns a count of 1. Checking the actual SQL it seems that the OR statements are not being nested within the AND, so it's checking to see if the date passed is in the table OR the times match up (which they do) rather than if the date matches AND either the <code>startTime</code> is the same as the chosen <code>startTime</code> or the <code>endTime</code> lies between the two.</p>
  
  <p>How can I write the query to incorporate this?</p>