Fix SQL query for recordviews widget

The existing query was not fully working as intended.
Also it did not work with proper MySQL sql_mode settings.

This is fixed by building a proper query which delivers expected and
deterministic results.

We now always have latest records first.

Also there is no need to fetch the sys_language_uid, as we only fetch
localized record if only one language is allowed. That way we can just
check configuration and use the configuration to do language overlay.

Relates:  #35
This commit is contained in:
Daniel Siepmann 2020-08-12 15:38:50 +02:00
parent 8f755f79f2
commit 27e0623794

View file

@ -127,8 +127,7 @@ class Recordviews implements ChartDataProviderInterface
foreach ($this->getRecordviewsRecords() as $recordview) { foreach ($this->getRecordviewsRecords() as $recordview) {
$record = $this->getRecord( $record = $this->getRecord(
$recordview['record_uid'], $recordview['record_uid'],
$recordview['record_table_name'], $recordview['record_table_name']
$recordview['sys_language_uid']
); );
if ( if (
@ -188,13 +187,16 @@ class Recordviews implements ChartDataProviderInterface
} }
$result = $this->queryBuilder $result = $this->queryBuilder
->selectLiteral('count(record) as total') ->selectLiteral(
->addSelect('record_uid', 'record_table_name', 'sys_language_uid') $this->queryBuilder->expr()->count('record', 'total'),
$this->queryBuilder->expr()->max('uid', 'latest')
)
->addSelect('record_uid', 'record_table_name')
->from('tx_tracking_recordview') ->from('tx_tracking_recordview')
->where(... $constraints) ->where(... $constraints)
->groupBy('record') ->groupBy('record', 'record_uid', 'record_table_name')
->orderBy('total', 'desc') ->orderBy('total', 'desc')
->addOrderBy('uid', 'desc') ->addOrderBy('latest', 'desc')
->setMaxResults($this->maxResults) ->setMaxResults($this->maxResults)
->execute(); ->execute();
@ -205,14 +207,13 @@ class Recordviews implements ChartDataProviderInterface
private function getRecord( private function getRecord(
int $uid, int $uid,
string $table, string $table
int $sysLanguageUid
): array { ): array {
$recordTypeField = $GLOBALS['TCA'][$table]['ctrl']['type'] ?? ''; $recordTypeField = $GLOBALS['TCA'][$table]['ctrl']['type'] ?? '';
$record = BackendUtility::getRecord($table, $uid); $record = BackendUtility::getRecord($table, $uid);
if ($sysLanguageUid > 0 && count($this->languageLimitation) === 1 && $record !== null) { if (count($this->languageLimitation) === 1 && $record !== null) {
$record = $this->pageRepository->getRecordOverlay($table, $record, $sysLanguageUid); $record = $this->pageRepository->getRecordOverlay($table, $record, $this->languageLimitation[0]);
} }
return [ return [