7.x entity.inc EntityFieldQuery::execute()

Executes the query.

After executing the query, $this->orderedResults will contain a list of the same stub entities in the order returned by the query. This is only relevant if there are multiple entity types in the returned value and a field ordering was requested. In every other case, the returned value contains everything necessary for processing.

Return value

Either a number if count() was called or an array of associative arrays of stub entities. The outer array keys are entity types, and the inner array keys are the relevant ID. (In most cases this will be the entity ID. The only exception is when age=FIELD_LOAD_REVISION is used and field conditions or sorts are present -- in this case, the key will be the revision ID.) The entity type will only exist in the outer array if results were found. The inner array values are always stub entities, as returned by entity_create_stub_entity(). To traverse the returned array:


    foreach ($query->execute() as $entity_type => $entities) {
      foreach ($entities as $entity_id => $entity) {
  

Note if the entity type is known, then the following snippet will load the entities found:

$result = $query
  ->execute();
if (!empty($result[$my_type])) {
  $entities = entity_load($my_type, array_keys($result[$my_type]));
}

File

includes/entity.inc, line 1207

Class

EntityFieldQuery
Retrieves entities matching a given set of conditions.

Code

public function execute() {

  // Give a chance to other modules to alter the query.
  drupal_alter('entity_query', $this);
  $this->altered = TRUE;

  // Initialize the pager.
  $this
    ->initializePager();

  // Execute the query using the correct callback.
  $result = call_user_func($this
    ->queryCallback(), $this);
  return $result;
}

Comments

kenorb’s picture

$result = $query->execute();

if (!empty($results['node'])) {
  $nids = array_keys($results['node']);
}

$first_nid = reset($nids);
$nodes = node_load_multiple($nids);