8.5.x database.inc db_or()
8.0.x database.inc db_or()
8.1.x database.inc db_or()
8.2.x database.inc db_or()
8.3.x database.inc db_or()
8.4.x database.inc db_or()
8.6.x database.inc db_or()
7.x database.inc db_or()

Returns a new DatabaseCondition, set to "OR" all conditions together.

Return value

DatabaseCondition

Related topics

9 calls to db_or()
hook_query_TAG_alter in modules/system/system.api.php
Perform alterations to a structured query for a given tag.
node_access in modules/node/node.module
Determines whether the current user may perform the operation on the node.
SearchQuery::executeFirstPass in modules/search/search.extender.inc
Executes the first pass query.
SearchQuery::parseSearchExpression in modules/search/search.extender.inc
Parses the search query into SQL conditions.
SearchQuery::setOption in modules/search/search.extender.inc
Applies a search option and removes it from the search query string.

... See full list

File

includes/database/database.inc, line 2721
Core systems for the database layer.

Code

function db_or() {
  return new DatabaseCondition('OR');
}

Comments

kutter’s picture

Lifted from (http://drupal.org/node/310086). Pasting it here in case others hit this page first looking for the same.

<?php
// From taxonomy_term_save():
$or = db_or()->condition('tid1', 5)->condition('tid2', 6);
db_delete('term_relation')->condition($or)->execute();
// DELETE FROM {term_relation} WHERE ((tid1 = 5 OR tid2 = 6))
?>
mcferren’s picture

What would be the correct method to build a loop within this db_or() so that I can pull an amount of conditions from a previous database query? Here is what I have concocted so far but it doesn't seem to work:

$query->condition(db_or()
foreach ($subjids as $subjid) {
"->condition ('s.field_subject_tid', " . $subjid . ", '=')"
}
);

zerbash’s picture

Try

$db_or = db_or(); 
foreach($subjids as $subjid){
     $db_or->condition('s.field_subject_tid',$subjid,'='); 
}
$query->condition($db_or);
eddie_c’s picture

Thanks zerbash. That worked really well for me.

phreestilr’s picture

Awesome, thank you!

davidwhthomas’s picture

Note you can also use $query->where($condition) for more complex and arbitrary where clauses, including "OR" conditions

e.g: Get all nodes where operating status field is null, or value of 0

<?php
  $query = db_select('node', 'n');
  $query->fields('n', array('nid', 'title'));
  $query->leftJoin('field_data_field_operating', 'f', 'n.nid = f.entity_id');
  $query->condition('n.type', 'resource');
  $query->condition('n.nid', array($nids), 'IN');
  $query->where("((f.field_operating_value IS NULL) OR (f.field_operating_value = 0))");
  drupal_set_message($query->__toString()); // DEBUG
  $results = $query->execute();
?>

DT