I've been reading the documentatoin on conditional clauses and also studied the api description for db_and() and db_or() but I fail to create a $query
with 3 ->condition
s:
- select all between 0 and 100
- but exclude 23, 48 and 97
- select also 107, 123, 126 and 128
That's what I have tried so far:
$query->db_or()
->condition('my_field', array(107, 123, 126, 128), 'IN')
->condition(db_and()->condition('my_field', array(0, 100), 'BETWEEN')
->condition('my_field', array(23, 48, 97), 'NOT IN'));
But I get the following error:
PHP Fatal error: Call to undefined method SelectQuery::db_or() in [... first line]
I've tried to play around with the syntax like that:
$query->condition(->db_or()
->condition('my_field', array(107, 123, 126, 128), 'IN')
->condition(db_and()->condition('my_field', array(0, 100), 'BETWEEN')
->condition('my_field', array(23, 48, 97), 'NOT IN')));
But again getting errors like that:
PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' in [... first line]
I also tried working with variables:
$cond01 = array('my_field', array(107, 123, 126, 128), 'IN');
$cond02 = array('my_field', array(0, 100), 'BETWEEN');
$cond03 = array('my_field', array(23, 48, 97), 'NOT IN');
$db_and = db_and();
$db_and->condition($cond02);
$db_and->condition($cond03);
$db_or = db_or();
$db_or->condition($cond01);
$db_or->condition($db_and);
$query->condition($db_or);
But this again yields drupal errors like that:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'
How to create a complex conditional database query with both AND and OR statements?