Commit b0e26356 authored by webchick's avatar webchick
Browse files

#331737 by Crell: Fix nested conditions (plus test)

parent 7aea369d
......@@ -1032,7 +1032,7 @@ public function compile(DatabaseConnection $connection) {
if ($condition['field'] instanceof QueryConditionInterface) {
// Compile the sub-condition recursively and add it to the list.
$condition['field']->compile($connection);
$condition_fragments[] = (string)$condition['field'];
$condition_fragments[] = '(' . (string)$condition['field'] . ')';
$arguments += $condition['field']->arguments();
}
else {
......
......@@ -1416,6 +1416,23 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase {
$this->assertTrue(FALSE, $e->getMessage());
}
}
/**
* Confirm that we can properly nest conditional clauses.
*/
function testNestedConditions() {
// This query should translate to:
// "SELECT job FROM {test} WHERE name = 'Paul' AND (age = 26 OR age = 27)"
// That should find only one record. Yes it's a non-optimal way of writing
// that query but that's not the point!
$query = db_select('test');
$query->addField('test', 'job');
$query->condition('name', 'Paul');
$query->condition(db_or()->condition('age', 26)->condition('age', 27));
$job = $query->execute()->fetchField();
$this->assertEqual($job, 'Songwriter', t('Correct data retrieved.'));
}
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment