API page: http://api.drupal.org/api/drupal/includes--entity.inc/function/EntityFie...

Dear all,

The documentation of 'Return Value' states:

    $result = $query->execute();
    $entities = entity_load($my_type, array_keys($result[$my_type]));

It seems that this only works when the result is nonempty. Otherwise $result[$my_type] causes an error message Notice: Undefined index: model in .... To fix this, you must explicitly check for empty results, not so elegant, but necessary it seems.

Kaspar

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Title: Documentation problem with EntityFieldQuery::execute » EntityFieldQuery::execute documentation - return value example doesn't work if query result is empty
Version: 7.x-dev » 8.x-dev
Issue tags: +Novice, +Needs backport to D7

Thanks for reporting this! I think a novice patch contributor could probably fix this.
It appears that it needs fixing in Drupal 8 first:
http://api.drupal.org/api/drupal/core--modules--entity--entity.query.inc...

I'm not sure why the Drupal 8 and 7 versions of this method don't link to each other... probably because the file name has changed. Anyway, that's a separate issue, which I'll file in the API module.

jhodgdon’s picture

For anyone interested, here's the issue about the methods not linking to each other:
#1429314: Other versions linking between branches on methods pages seems to be broken

chris.leversuch’s picture

Status: Active » Needs review
FileSize
1.86 KB

I've added "The entity type will only exist in the outer array if results were found." to the return description and an if statement has been added to the sample code.

I've also adjusted the line wrap on the first line as the text was wrapping too early - I can remove this change if it shouldn't be included.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks!

Dries’s picture

Version: 8.x-dev » 7.x-dev

Committed to 8.x. Thanks. Moving to 7.x.

chris.leversuch’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.8 KB

D7 version.

chris.leversuch’s picture

FileSize
1.8 KB

Renamed for testing.

jhodgdon’s picture

Status: Needs review » Needs work

This patch is not the same as th D8 patch. It has a typo "In most this cases this...".

chris.leversuch’s picture

I knew there was something else I fixed in the D8 version. This one should be correct.

chris.leversuch’s picture

Status: Needs work » Needs review

Change status.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks -- now the patches are the same (except context). :)

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x. My first commit! :)

webchick’s picture

YAYYY! :D Welcome, Jennifer!!

Automatically closed -- issue fixed for 2 weeks with no activity.