mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
LazyCriteriaCollection::count returns ambiguous results when paired with Criteria::setMaxResults #7016
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 @ScopeyNZ on GitHub (Aug 3, 2022).
Bug Report
Summary
When using
LazyCriteriaCollection::count()when a criteria withsetMaxResultsis configured, the result is ambiguous, depending on whether the collection has been initialized or not.How to reproduce
Assuming you have an entity
MyEntitywith at least 2 records:Expected behavior
Either 2 or 1 shows every time, but ideally it's consistent. Either always perform a query for count, or do a
min($count, $criteria->getMaxResults());, I'm not sure which is intended.Happy to put forward a PR if there's a preferred expected behaviour. I'd also like to know if it would have to go against 3.x as I'm not sure sure if it'd be considered as a BC break to fix.
@derrabus commented on GitHub (Aug 7, 2022):
First of all, please be a bit more precise about the ORM version you're using. "2.x" is literally every Doctrine ORM release we've ever had. 😬
My expectation would be that
count()consistently returns$maxResultsif the actual count is higher. A PR would be welcome.@ScopeyNZ commented on GitHub (Aug 8, 2022):
Sorry I discovered the problem in 2.7.5. Verified it still appeared to be an issue in 2.12.3, and looking back it appeared to be an issue before 2.7.5 as well.
I probably should've put
>2.7.5 (at least)@Aweptimum commented on GitHub (Jun 7, 2023):
For what it's worth, I had noticed this behavior as well and made a dupe (#10766) and jumped the gun on submitting #10767. I changed the
BasicEntityPersister->getCountSQLto use a subselect + limit in my fork so that the count would be consistent with the criteria's max results if set. If maxResults isn't set, it makes sense to count the entire table because that's effectively what the user is asking for. I had to change the expected results in BasicEntityPersisterTypeValueSqlTest->testCountCondition, but it didn't break any other tests.