throw an exception when IDs passed to loadMultiple() are badly formed

Created on 30 June 2021, over 3 years ago
Updated 5 August 2024, 5 months ago

Problem/Motivation

There are lots of issues about ContentStorageBase producing a warning 'Warning: array_flip(): Can only flip STRING and INTEGER values'.

This happens here:

    $flipped_ids = $ids ? array_flip($ids) : FALSE;

If the $ids array can't be flipped, then that means it contains things that aren't valid entity IDs, such as arrays. So the parameter passed in is incorrect.

It also means the entity load isn't going to work as expected.

The method should throw an exception to allow for better DX.

(Adding related issues which propose different methods for improving this.)

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Needs work

Version

11.0 🔥

Component
Entity 

Last updated about 16 hours ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇬🇧United Kingdom hugronaphor

    Edge-case:

    I have 0, 1 or 2 entities to load and the order matters in my case.
    For performance reasons I want to load them `loadMultiple()`

    $nodeIds = [
      $queryPrev->execute()->fetchField(),
      $queryNext->execute()->fetchField(),
    ];
    $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nodeIds);
    return [
      '#prev' => !empty($nodes[$nodeIds[0]]) ? $nodes[$nodeIds[0]] : NULL,
      '#next' => !empty($nodes[$nodeIds[1]]) ? $nodes[$nodeIds[1]] : NULL,
    ];
    

    To avoid the warning you need extensive logic. E.g:

    if (count(array_filter($nodeIds)) === 2) {
      $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nodeIds);
    }
    elseif (!empty($nodeIds[0])) {
      $nodes = [
        $this->entityTypeManager->getStorage('node')->load($nodeIds[0]),
        NULL,
      ];
    }
    elseif (!empty($nodeIds[1])) {
      $nodes = [
        NULL,
        $this->entityTypeManager->getStorage('node')->load($nodeIds[1]),
      ];
    }
    

    But if you would have 3, 4, 5 entities to load multiple single loads would be needed.

    I'm not demanding changes, just wanted to point out to this DX

  • Status changed to Needs review 5 months ago
  • 🇮🇳India rakshith.thotada

    Rerolled the patch for Drupal 10.2.x

  • Status changed to Needs work 5 months ago
  • 🇺🇸United States smustgrave

    Current development branch is 11.x and changes should be in MRs

Production build 0.71.5 2024