0

I have the following query:

$query = \Drupal::entityQuery('node')->condition('type','article');
$result = $query->execute();

Then I want to access all content:

$nodes = \Drupal\node\Entity\Node::loadMultiple(array_keys($result));

But in variable $results, there are wrong NIDs. My first article ID is '12' and second article '13'. But in $result are '9' and '10'

Do you have an explanation for this?

Ok, I found out I can't use array_keys... because keys are not same as IDs. Proved just now in an easy example... in my $result[9] there is the right id '12'.

0

The return value of Query::execute,

int|array Returns an integer for count queries or an array of ids. The values of the array are always entity ids. The keys will be revision ids if the entity supports revision and entity ids if not.

Nodes support revisions so the keys will be revision ids and not entity ids.

$nodes = \Drupal\node\Entity\Node::loadMultiple(array_values($result));

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.