Carey Estes
Carey Estes

Reputation: 1564

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.

Upvotes: 2

Views: 2321

Answers (1)

j0k
j0k

Reputation: 22756

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();

Upvotes: 4

Related Questions