mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
QueryBuilder IndexBy not indexing a result set #7288
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @SergeC on GitHub (Dec 23, 2023).
Bug Report
Summary
QueryBuilder ignores indexBy argument and returns the result set indexed by PHP's array-keys.
Current behavior
QueryBuilder returns the result set indexed by PHP's array-keys.
How to reproduce
Expected behavior
QueryBuilder should return the result set indexed by values in the database, specified in the 2nd argument to
createQueryBuilder.Installed dependencies:
Actual result:


Expected result:
@greg0ire commented on GitHub (Dec 23, 2023):
Assuming
$thisis an entityrepository instance,createQueryBuilderalready callsselectinternally. With your call, you're replacing that select, making surej.idis no longer selected.@SergeC commented on GitHub (Dec 23, 2023):
In this use-case, I fetch only one column. Which means I need to specify it in select. Doctrine should automatically add indexing column to select statement. Even when I add indexing column to select - nothing changes.


@greg0ire commented on GitHub (Dec 23, 2023):
Huh, I did not expect that. Maybe it has to do with
getSingleColumnResult(). Can you try other possibilities?@SergeC commented on GitHub (Dec 23, 2023):
@greg0ire commented on GitHub (Dec 23, 2023):
I meant with
j.idselected@SergeC commented on GitHub (Dec 24, 2023):
This adds id to the result set along with array keys.
@greg0ire commented on GitHub (Dec 24, 2023):
Okay. This means this issue really is about index by +
getSingleColumnResult.@beberlei commented on GitHub (Dec 26, 2023):
I dont understand, where is index by configured here? Maybe i missef it, but this is only a non entity query here, getSingleColumnResult has no index by behavior afair
@greg0ire commented on GitHub (Dec 26, 2023):
The second argument of
EntityRepository::createQueryBuilder()configures it:f80ef66ffb/lib/Doctrine/ORM/EntityRepository.php (L76-L89)@SergeC commented on GitHub (Feb 5, 2024):
@greg0ire @beberlei Any update on this? Any other info needed from my side?
@jurchiks commented on GitHub (Jun 17, 2024):
I just stumbled upon this, good thing I decided to google it.
It is stupid that something like this happens; one would think that
indexByis added implicitly regardless of what the user does withselect(), because it is REQUIRED by the code. It should NOT be overridable by user.I was wondering why my "account balance mapped by account ID" query was returning 0-indexed results, made no goddamn sense, and the subsequent code was clearly doing the wrong thing, when it absolutely shouldn't have. Turns out the problem wasn't in my code, but in Doctrine being dumb yet again... Such an overcomplicated mass of code, and yet.
@rasstislav commented on GitHub (Sep 19, 2024):
@greg0ire Hello. Anything new on this issue?
@greg0ire commented on GitHub (Sep 19, 2024):
@rasstislav not as far as I can tell. Do you want to work on a fix?
@Asenar commented on GitHub (Nov 29, 2024):
Hi @greg0ire , I have the same issue and I would like to contribute, but I'm not sure where to begin.
Is it a good start to write the test in BasicFunctionalTest ?
@greg0ire commented on GitHub (Nov 29, 2024):
Yes, it's a good start 👍
@Arkemlar commented on GitHub (Oct 25, 2025):
Having to add array_column() cycle on the result dataset because of this, not too cool workaround because it is not OK to do so with large datasets.