2

I have some node ids from entityfieldquery. I would like to get the values of a field from all of these nodes. Should I use db_query, or should do a node_load_multiple, and get the field value with a foreach? db_query seems more efficient.

1

You can use field_attach_load() which will load the fields for a group of entities. You can also pass in the field_id value so it doesn't load unnecessary fields. I benchmarked field_attach_load() against entity_load() (which is what node_load_multiple will call) and the former was approximately ten times faster.

Here is an example:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'post')
  ->propertyCondition('status', 1);
$result = $query->execute();

if (isset($result['node'])) {
  // field_attach_load() requires entities to be passed on it with the bundle 
  // and the ID instead of just IDs. We need to add these values to the result
  // of EntityFieldQuery()
  foreach ($result['node'] as &$node) {
    $node->bundle = $node->type;
    $node->id = $node->nid;
  }
  field_attach_load('node', $result['node'], FIELD_LOAD_CURRENT, array('field_id' => 'body'));
}
| improve this answer | |
-1

ya u r right. node_load_multiple fires huge pressure on db and it can effect on ur site's performance. So, always use db_select or db_query for ur use, if there is no option available then only use loads....

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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