Sql – Doctrine – How to ask WHERE cond1 AND ( cond2 OR cond3)

doctrinesqlsymfony1

While using Doctrine in a Symfony project I've come into the situation where I need to apply a condition and then one of two following conditions (which both happen to be sub-queries). I know of the andWhere() and orWhere() functions, but am having trouble using these to generate things like:
WHERE cond1 AND ( cond2 OR cond3)

Best Answer

You can do this whith DQL:

$models = Doctrine::getTable('ModelName')
    ->findByDql(
        'field_one = ? AND (field_two = ? OR field_three = ?)',
        array('cond_1','cond_2', 'cond_3')
    );

So $models will be a Doctrine_Collection with all found elements.

Related Topic