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
Reviewed-on: https://review.typo3.org/51320

Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: Manuel Selbach's avatarManuel Selbach <manuel_selbach@yahoo.de>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 69daabbf
......@@ -416,13 +416,22 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface
}
$queryBuilder = $this->objectManager->get(Typo3DbQueryParser::class)
->convertQueryToDoctrineQueryBuilder($query);
->convertQueryToDoctrineQueryBuilder($query)
->resetQueryPart('orderBy');
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');
$queryBuilder->resetQueryPart('groupBy')
->selectLiteral(sprintf('COUNT(DISTINCT %s.%s)', $tableName, $fieldName));
} else {
$queryBuilder->count('*');
}
try {
$count = $queryBuilder->resetQueryPart('orderBy')
->count('*')
->execute()
->fetchColumn(0);
$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('tagsSpecial.name', 'SpecialTagForAuthor1')
)
);
$this->assertSame(3, $query->count());
$this->assertSame(4, $query->count());
}
}
......@@ -24,4 +24,12 @@
<tags>0</tags>
<tags_special>1</tags_special>
</tx_blogexample_domain_model_person>
<tx_blogexample_domain_model_person>
<uid>4</uid>
<pid>0</pid>
<firstname>Another Author</firstname>
<lastname>With special tag</lastname>
<tags>0</tags>
<tags_special>1</tags_special>
</tx_blogexample_domain_model_person>
</dataset>
......@@ -32,4 +32,12 @@
<sorting>1</sorting>
<sorting_foreign>1</sorting_foreign>
</tx_blogexample_domain_model_tag_mm>
</dataset>
\ No newline at end of file
<tx_blogexample_domain_model_tag_mm>
<uid_local>4</uid_local>
<uid_foreign>13</uid_foreign>
<tablenames>tx_blogexample_domain_model_person</tablenames>
<fieldname>tags_special</fieldname>
<sorting>1</sorting>
<sorting_foreign>1</sorting_foreign>
</tx_blogexample_domain_model_tag_mm>
</dataset>
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