Issue Bug Resolved

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

Anonymous 03/27/17, 10:39 updated 10/18/26, 14:35

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

History 79

  1. Julian Hofmann commented and changed TYPO3 Version 7 8 <span data-processor="relativetime" data-value="2017-05-03T09:15:28+00:00" > 05/03/17, 09:15 </span>

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

  2. 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;

  3. 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 when count() 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, calls count() before iterating, and asserts that the count matches the returned objects and not the table total.

    A statement passed as a Doctrine QueryBuilder does not take this branch and is still counted wrongly. That case is tracked in #93576.

    Once 96788 is merged, this issue can be closed.

  4. Anonymous commented and changed status Under Review Resolved done 0 % 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.