Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFieldQuery multiple alternative conditions

Tags:

drupal-7

Is there more optimal and shorter way to get nodes with certain conditions?

$query = new EntityFieldQuery;
$result = $query
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('type', $node_type)
  ->propertyCondition('title', $title)
  ->fieldCondition('field_number', 'value', '1', '=');
  ->propertyCondition('status', 1, '=')
  ->execute();

// $result['node'] contains a list of nids where the title matches
if (!empty($result['node']) {
  // You could use node_load_multiple() instead of entity_load() for nodes
  $nodes = entity_load('node', array_keys($result['node']));
}

$query_two = new EntityFieldQuery;
$result_two = $query_two
  ->entityCondition('entity_type', 'node')
  ->propertyCondition('type', $node_type)
  ->propertyCondition('title', $title)
  ->fieldCondition('field_number', 'value', '2', '=');
  ->propertyCondition('status', 1, '=')
  ->execute();

// $result_two['node'] contains a list of nids where the title matches
if (!empty($result_two['node']) {
  // You could use node_load_multiple() instead of entity_load() for nodes
  $nodes_two = entity_load('node', array_keys($result_two['node']));
}
like image 223
Anglo Avatar asked May 16 '11 12:05

Anglo


1 Answers

Well, you certainly could use ->fieldCondition('field_number', 'value', array(1, 2)), but other than that, not that I know of (and I wrote EntityFieldQuery). Even if you were to rewrite this to an SQL-storage only query, it would not be much simpler.

You do not need to specify = as the operator and you do not need to specify IN either, they are defaults for a scalar / array value.

like image 145
chx Avatar answered Sep 19 '22 04:09

chx