Commit 728afa52 authored by Morton Jonuschat's avatar Morton Jonuschat Committed by Benni Mack
Browse files

[BUGFIX] Extbase: Fix counting objects on joined tables

While it's normally fine to replace DISTINCT with GROUP BY when counting
objects this results in a different resultset. The GROUP BY statement will
have n rows with the count per grouped combination while the DISTINCT
based query will have one row with the count of unique combinations.

Use a COUNT(DISTINCT uid) to get the expected result. 
DISTINCT is supported on MySQL, PostgreSQL, Oracle and MSSQL Server.

Change-Id: If3193113529f1bb01693dba39abccfe349f169b1
Resolves: #79330
Releases: master

Tested-by: default avatarTYPO3com <>
Reviewed-by: Manuel Selbach's avatarManuel Selbach <>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <>
Tested-by: Christian Kuhn's avatarChristian Kuhn <>
Reviewed-by: Benni Mack's avatarBenni Mack <>
Tested-by: Benni Mack's avatarBenni Mack <>
parent 69daabbf
......@@ -416,13 +416,22 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface
$queryBuilder = $this->objectManager->get(Typo3DbQueryParser::class)
if (count($queryBuilder->getQueryPart('groupBy')) !== 0) {
$source = $queryBuilder->getQueryPart('from')[0];
// Tablename is already quoted for the DBMS, we need to treat table and field names separately
$tableName = $source['alias'] ?: $source['table'];
$fieldName = $queryBuilder->quoteIdentifier('uid');
->selectLiteral(sprintf('COUNT(DISTINCT %s.%s)', $tableName, $fieldName));
} else {
try {
$count = $queryBuilder->resetQueryPart('orderBy')
$count = $queryBuilder->execute()->fetchColumn(0);
} catch (DBALException $e) {
throw new SqlErrorException($e->getPrevious()->getMessage(), 1472074379);
......@@ -235,6 +235,6 @@ class CountTest extends \TYPO3\CMS\Components\TestingFramework\Core\FunctionalTe
$query->equals('', 'SpecialTagForAuthor1')
$this->assertSame(3, $query->count());
$this->assertSame(4, $query->count());
......@@ -24,4 +24,12 @@
<firstname>Another Author</firstname>
<lastname>With special tag</lastname>
......@@ -32,4 +32,12 @@
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment