Drupal - Using OR with EntityFieldQuery
I have seen a solution of this problem. The idea is to use addTag()
in query and implement hook_query_TAG_alter()
, where you have good old SelectQuery
object.
You can sublass EntityFieldQuery
and override some methods.
The conditions that are added to an object of class EntityFieldQuery
(e.g. a property condition) are added to an array.
public function propertyCondition($column, $value, $operator = NULL) {
// The '!=' operator is deprecated in favour of the '<>' operator since the
// latter is ANSI SQL compatible.
if ($operator == '!=') {
$operator = '<>';
}
$this->propertyConditions[] = array(
'column' => $column,
'value' => $value,
'operator' => $operator,
);
return $this;
}
When the query is built, that array is then used in a loop similar to the following one (the code is present in EntityFieldQuery::propertyQuery()):
foreach ($this->propertyConditions as $property_condition) {
$this->addCondition($select_query, "$base_table." . $property_condition['column'], $property_condition);
}
$select_query
contains the value returned from a call to db_select()
.
You can't I'm afraid, ORs are not natively supported by the EntityFieldQuery
class.
One way round it might be to add a tag to the query with with ->addTag()
, then implement hook_query_TAG_alter()
to change the internal structure of the query manually for queries containing that tag.
Doing this you will be able to loop through the existing conditions and make the necessary alterations to add your OR
logic. It's not a pretty way to do it though; you can find an example here.