#80464count() not working correctly in Extbase when using custom statement
Description
When creating a Query object and assigning a custom statement, the count() method in QueryResult might not work correctly (depending on the query).
Once the QueryResult is initilialized, count() will return the correct result since it will use result array.
If count() is called before that, the error appears in TYPO3\CMS\Extbase\Persistence\Generic\Storage::getObjectCountByQuery()
In my case, i have a query on an extension table where I left join another table of the same extension (as the extension is not public it would be no use to add the statement here).
The problem is: the joined table, a condition referencing the joined table as well as one (the last) condition referencing the main table are ignored within getObjectCountByQuery().
The result is that getObjectCountByQuery() effectively returns the raw count of all records in the main table that are not deleted or hidden (in this case).
If count() is not supposed to work with custom statements, this should be annotated in the code and I would prefer returning “NULL” or “false” as a default in that case – though I can’t imagine this is intended behavior.
There appear to be other cases where count() does not work correctly with custom statement, so this might not be limited to left joins:
https://www.typo3.net/forum/thematik/zeige/thema/108484/
Reviews 3
Related issues 3
History 79
-
Julian Hofmann commented and changed TYPO3 Version
78 <span data-processor="relativetime" data-value="2017-05-03T09:15:28+00:00" > 05/03/17, 09:15 </span> -
Julian Hofmann changed Related to #80820 <span data-processor="relativetime" data-value="2017-05-03T09:54:02+00:00" > 05/03/17, 09:54 </span>
-
Sven Burkert commented <span data-processor="relativetime" data-value="2019-04-16T11:18:07+00:00" > 04/16/19, 11:18 </span>
I also had this bug (in TYPO3 7.6.31), even for simple (or invalid!) queries like:
$statement = ‘SELECT * from mytable WHERE uid=1’;
$statement = ‘SELECT * from notexistingtable’;This helped:
$result = $query->statement($statement)->execute();
$result->current();
return $result; -
Mathias Brodala changed Related to #87655 <span data-processor="relativetime" data-value="2020-09-15T12:24:48+00:00" > 09/15/20, 12:24 </span>
-
Michael Grundkötter changed Related to #93576 <span data-processor="relativetime" data-value="2021-02-24T07:50:47+00:00" > 02/24/21, 07:50 </span>
-
Gerrit Code Review pushed patch set 1 for main to change 96788 status
NewUnder Review <span data-processor="relativetime" data-value="2026-10-17T14:05:37+00:00" > 10/17/26, 14:05 </span> -
Alexander Schnitzler commented <span data-processor="relativetime" data-value="2026-10-17T14:14:44+00:00" > 10/17/26, 14:14 </span>
I think this is resolved for custom SQL statements.
Since 3ed515fbb31 “[BUGFIX] get count consistent with execute for self written statements” (54844, #82910), @Typo3DbBackend::getObjectCountByQuery()@ has its own branch for string statements: it executes the statement and counts the returned rows, instead of running a
COUNT(*)on the main table. JOINs and WHERE conditions of the statement are therefore respected, also whencount()is called before the result is iterated. The fix is part of 9.1.0; on 8.7 it was merged as bf8f174e113 (55214) and released with 8.7.10.The fix came without a test. 96788 adds
CountTest::countOfQueryWithJoinAndWhereStatementMatchesReturnedObjects. The test uses a statement with a LEFT JOIN and a WHERE condition on the joined table, callscount()before iterating, and asserts that the count matches the returned objects and not the table total.A statement passed as a Doctrine
QueryBuilderdoes not take this branch and is still counted wrongly. That case is tracked in #93576.Once 96788 is merged, this issue can be closed.
-
Gerrit Code Review pushed patch set 2 for main to change 96788 <span data-processor="relativetime" data-value="2026-10-17T14:19:09+00:00" > 10/17/26, 14:19 </span>
-
Anonymous commented and changed status
Under ReviewResolved done0 %100 % <span data-processor="relativetime" data-value="2026-10-18T14:35:16+00:00" > 10/18/26, 14:35 </span>Applied in changeset commit:28cbf13d3faa8b77808070789b85f1c3b631db3d.
smichaelsen has written an extension of QueryResult which solves the problem: https://gist.github.com/smichaelsen/2bfdfb872d04a0776f01