1

I have an EFQ that finds the users whose field values match a given array of values:

  $users_query = new EntityFieldQuery();

  $users_query->entityCondition('entity_type', 'user')
    ->fieldCondition('field_my_field', 'value', $array_of_values);

  $users_query_result = $users_query->execute();

This will create a list of uid's but I need to know which uid goes with which value in the array.

2 Answers 2

1

EFQ will return an entity stub, if you want the fully loaded entities, you can try something like:

// Build query
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user');
$query->fieldCondition('field_my_field', 'value', $values);
// Execute for result
$result = $query->execute();
// If has data
if (!empty($result['user'])) {
  // Load users
  $accounts = entity_load('user', array_keys($result['user']));
  // If using devel
  // dpm($accounts);
}else{
  // No results
}
2
  • This is essentially what I ended up doing with user_load_multiple. How much of a performance hit is it to load entire users instead of just that field? Commented Jan 21, 2013 at 2:46
  • 1
    Loading the full user object is more performance intensive than loading a single field. Normally, you'd use EFQ if you want the actual entities, if you just want some basic field data, a simple db_select or db_query may suffice as a more lightweight solution. Commented Jan 21, 2013 at 2:52
1

The only way I suggest is loading the user objects with entity_load(), or user_load_multiple() using the array returned from $users_query->execute(). That is the only method that always work.
On Drupal, entity fields can be stored on a different database engine, or on a Mongodb database; directly querying the database could not work, similarly to directly querying the database instead of using cache_get(), or cache_set().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.