2

Good day to everyone!

some help needed with fetching result out of EntityFieldQuery. Have a unique value of the field. I try to get 'nid' of node by value of the field with help of EntityFieldQuery. my problem is that i dont know how to fetch result in a right way to get just 'nid'. then knowing nid i will be able to load the node and do some work with it.

$query = new EntityFieldQuery;
$entities = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'order')
->fieldCondition('field_reference_number', 'value', $titleref)
->execute();
$result= $query->execute();
drupal_set_message('nid: ' .print_r($result, true));

my result is:

nid: Array ( [node] => Array ( [2250] => stdClass Object ( [nid] => 2250 [vid] => 2252 [type] => order ) ) ) 

how to fetch it corectly to get 'nid' out of it?

Thanks for your time

1 Answer 1

2

The simplest way would be:

$result = $query->execute();

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

There are a few different ways to get the first nid out of the array if that's what you need; personally I prefer reset() as it's non-destructive to the original array (unlike array_shift() for example).

$first_nid = reset($nids);

If you then need the node objects you can pass that array straight to node_load_multiple():

$nodes = node_load_multiple($nids);
2
  • 1
    wasn't it too fast to answer ;)
    – user12068
    Jan 10, 2013 at 11:22
  • @Mark I have to write pretty much identical code 20 times a day so it's stuck in my brain :)
    – Clive
    Jan 10, 2013 at 11:28

Your Answer

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

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