count() not working correctly in Extbase when using custom statement
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:
Updated by Julian Hofmann about 4 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 Sven Burkert over 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';
$result = $query->statement($statement)->execute();