Bug #80464

count() not working correctly in Extbase when using custom statement

Added by Christian Wellinghorst almost 4 years ago. Updated almost 2 years ago.

Must have
Target version:
Start date:
Due date:
% Done:


Estimated time:
TYPO3 Version:
PHP Version:
Is Regression:
Sprint Focus:


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:

Related issues

Related to TYPO3 Core - Bug #80820: Extbase - Wrong size of QueryResult object after make statementClosed2017-04-12

Related to TYPO3 Core - Bug #87655: Wrong count by extbase with own QueryBuilderAccepted2019-02-05


Updated by Julian Hofmann over 3 years ago

  • TYPO3 Version changed from 7 to 8

smichaelsen has written an extension of QueryResult which solves the problem: https://gist.github.com/smichaelsen/2bfdfb872d04a0776f01


Updated by Julian Hofmann over 3 years ago

  • Related to Bug #80820: Extbase - Wrong size of QueryResult object after make statement added

Updated by Sven Burkert almost 2 years ago

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();
return $result;


Updated by Mathias Brodala 4 months ago

  • Related to Bug #87655: Wrong count by extbase with own QueryBuilder added

Also available in: Atom PDF