title: Complex WHERE clauses using the PHP Doctrine ORM
body: <p>I'm using the PHP Doctrine ORM to build my queries.  However, I can't quite seem to figure how to write the following WHERE clause using DQL (Doctrine Query Language):</p>
  
  <pre><code>WHERE name='ABC' AND (category1 = 'X' OR category2 = 'X' OR category3 = 'X') 
  AND price &gt; 10
  </code></pre>
  
  <p>How can I specify where the parentheses go?</p>
  
  <p>What I currently have in my PHP code is this:</p>
  
  <pre><code>-&gt;where('name = ?', 'ABC')
  -&gt;andWhere('category1 = ?', 'X')
  -&gt;orWhere('category2 = ?', 'X')
  -&gt;orWhere('category3 = ?', 'X')
  -&gt;andWhere('price &gt; ?', 10)
  </code></pre>
  
  <p>But this produces something like</p>
  
  <pre><code>WHERE name='ABC' AND category1 = 'X' OR category2 = 'X' OR category3 = 'X' 
  AND price &gt; 10
  </code></pre>
  
  <p>which, due to order of operations, doesn't return the intended results.</p>
  
  <p>Also, is there a difference between the "where", "andWhere", and "addWhere" methods?</p>
  
  <p><strong>UPDATE</strong>
  Ok, it seems like you can't do complex queries using DQL, so I've been trying to write the SQL manually and use the andWhere() method to add it.  However, I'm using WHERE..IN and Doctrine seems to be stripping out my enclosing parentheses:</p>
  
  <pre><code>$q-&gt;andWhere("(category1 IN $subcategory_in_clause
              OR category2 IN $subcategory_in_clause 
              OR category3 IN $subcategory_in_clause)");
  </code></pre>