Problem/Motivation

I noticed that an entity query using count() returns a string instead of an int, which contradicts the return type of int|array in \Drupal\Core\Entity\Query\Sql\Query.php:

  /**
   * Executes the query and returns the result.
   *
   * @return int|array
   *   Returns the query result as entity IDs.
   */
  protected function result() {
    if ($this->count) {
      return $this->sqlQuery->countQuery()->execute()->fetchField();
    }
    // Return a keyed array of results. The key is either the revision_id or
    // the entity_id depending on whether the entity type supports revisions.
    // The value is always the entity id.
    return $this->sqlQuery->execute()->fetchAllKeyed();
  }

Steps to reproduce

    // Returns a string instead of an int.
    $count = \Drupal::entityTypeManager()->getStorage('user')->getQuery()
      ->count()
      ->execute();

Proposed resolution

Cast the count result to an integer. This will be good in preparation for using return type hints in the future.

  /**
   * Executes the query and returns the result.
   *
   * @return int|array
   *   Returns the query result as entity IDs.
   */
  protected function result() {
    if ($this->count) {
      return (int) $this->sqlQuery->countQuery()->execute()->fetchField();
    }
    // Return a keyed array of results. The key is either the revision_id or
    // the entity_id depending on whether the entity type supports revisions.
    // The value is always the entity id.
    return $this->sqlQuery->execute()->fetchAllKeyed();
  }

Remaining tasks

Find if a similar problem exists anywhere else in code?

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3320240

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

solideogloria created an issue. See original summary.

solideogloria’s picture

Issue summary: View changes
solideogloria’s picture

Status: Active » Needs review

Changes have been pushed to the issue fork.

tobiasb’s picture

Version: 9.4.x-dev » 9.5.x-dev
Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Issue tags: +Needs tests

There should be somewhere in the entity query tests that we can change to an assertSame or remove a cast to prove that this is working as expected.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Needs work for #6

solideogloria’s picture

Maybe something could be added to SqlContentEntityStorageTest.php? But I don't know enough to do that.

alexpott’s picture

\Drupal\KernelTests\Core\Entity\EntityQueryTest change all the assertEquals to assertSame for the count queries.

solideogloria’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
smustgrave’s picture

Version: 9.5.x-dev » 10.1.x-dev
Status: Needs review » Needs work

Can you update the MR to point to 10.1 please.

solideogloria’s picture

How do I do that? I don't know how to switch which branch a MR is targeting.

smustgrave’s picture

If you're the author of the MR. On https://git.drupalcode.org/project/drupal/-/merge_requests/2940 you should see an edit button.
Then on the next screen where it says "From xyz into project/xyz" there should be a dropdown where you can select the target branch.

solideogloria’s picture

Okay, I did that. But now the merge includes a lot of things that aren't related.

solideogloria’s picture

Is it possible to keep only the committed changes, rather than merging 9.4 into 10.1 or whatever it's doing?

smustgrave’s picture

Do you have a copy of the changes you actually did? Can try and fix this but want to make sure your fix/tests are captured somewhere.

Essentially the MR branch is so far behind it's showing 1000s of changes so it needs to be rebased.

smustgrave’s picture

Another option is just close this MR and open a new one for 10.1 with your changes on that.

smustgrave’s picture

Okay since the MR is so far behind going to close this one and reopen a new one with those changes from those commits.

Know this was a pain, find it to be the one negative about using MRs vs patches but most likely this change would go into 10.1 now.

solideogloria’s picture

Status: Needs work » Needs review
solideogloria’s picture

Looks ready to merge now.

smustgrave’s picture

Changes look good once green I’ll mark it

Thanks!

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Changes look good and believe the test changes cover this.

  • catch committed bb13ca4 on 10.0.x
    Issue #3320240 by solideogloria, tobiasb, smustgrave, alexpott: Entity...
  • catch committed c189149 on 10.1.x
    Issue #3320240 by solideogloria, tobiasb, smustgrave, alexpott: Entity...
  • catch committed f339daf on 9.5.x
    Issue #3320240 by solideogloria, tobiasb, smustgrave, alexpott: Entity...
catch’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x, cherry-picked back to 9.5.x, thanks!

mfb’s picture

Issue tags: +Needs change record

I'd recommend a change record here, since this could cause tests to fail if calling assertSame()

tobiasb’s picture

First minimal draft of a CR https://www.drupal.org/node/3324755.

longwave’s picture

Issue tags: -Needs change record

Expanded the change record a bit and published it, thanks.

Status: Fixed » Closed (fixed)

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