Search code examples
phpdoctrinedoctrine-query

How to write a Doctrine Query with multiple conditions?


I am trying to write a doctrine query that meets the following conditions:

published = 1
AND 
subsection = "gui" OR "lvo" OR "tnn" OR "wer"

This is what I have:

$getPrograms = Doctrine::getTable('Program')
  ->createQuery()
  ->where('published=?', '1')
  ->andWhere('subsection=?', 'gui')
  ->orWhere('subsection=?', 'lvo')
  ->orWhere('subsection=?', 'tnn')
  ->orWhere('subsection=?', 'wer')
  ->orderBy('title ASC')
  ->execute();

This is pulling the correct subsection records, but is pulling records all published records, instead of only ones set to 1.

I feel I need to isolate the two conditions ( this AND (this OR this OR this)) but I do not know how.


Solution

  • Put your OR in the andWhere :

    $getPrograms = Doctrine::getTable('Program')
      ->createQuery()
      ->where('published=?', '1')
      ->andWhere(
        'subsection=? OR subsection=? OR subsection=? OR subsection=?', 
        array('gui', 'lvo', 'tnn', 'wer')
      )
      ->orderBy('title ASC')
      ->execute();