Skip to content

Commit

Permalink
Merge pull request #4 from andersundsehr/feature/one-to-n-relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti authored Jul 1, 2024
2 parents d3e10b8 + d7666f0 commit b5ac82c
Show file tree
Hide file tree
Showing 2 changed files with 886 additions and 711 deletions.
48 changes: 44 additions & 4 deletions Classes/DataProcessing/RelationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
$foreignTable = $tcaConfig['foreign_table'] ?? throw new RuntimeException('TCA config foreign_table not found');

if (isset($tcaConfig['foreign_field'])) {
$rows = $this->getRowsForeignField($tcaConfig, $uid);
} else {
$rows = $this->getRowsForeignField($tcaConfig, $foreignTable, $uid);
} elseif (isset($tcaConfig['MM'])) {
$rows = $this->getRowsMM($tcaConfig, $foreignTable, $uid);
} else {
$rows = $this->getRowsLocalField($tcaConfig, $foreignTable, $table, $field, $uid);
}

$records = [];
Expand Down Expand Up @@ -102,9 +104,8 @@ public function getRelation(ContentObjectRenderer $cObj, string $table, string $
* @param array<string, mixed> $tcaConfig
* @return list<array<string, string|int|float|bool>>
*/
private function getRowsForeignField(array $tcaConfig, int $uid): array
private function getRowsForeignField(array $tcaConfig, string $foreignTable, int $uid): array
{
$foreignTable = $tcaConfig['foreign_table'];
$foreignField = $tcaConfig['foreign_field'];
$foreignSortby = $tcaConfig['foreign_sortby'] ?? null;
$maxitems = (int)($tcaConfig['maxitems'] ?? 0);
Expand All @@ -126,6 +127,45 @@ private function getRowsForeignField(array $tcaConfig, int $uid): array
return $queryBuilder->executeQuery()->fetchAllAssociative();
}

/**
* @param array<string, mixed> $tcaConfig
* @return list<array<string, string|int|float|bool>>
*/
private function getRowsLocalField(array $tcaConfig, string $foreignTable, string $table, string $field, int $uid): array
{
$maxitems = (int)($tcaConfig['maxitems'] ?? 0);
if (($tcaConfig['renderType'] ?? null) === 'selectSingle') {
$maxitems = 1;
}

$queryBuilder = $this->connectionPool->getQueryBuilderForTable($foreignTable);
$expr = $queryBuilder->expr();
$whereUidMatched = $expr->inSet('local.' . $field, $queryBuilder->quoteIdentifier('relation.uid'), true);
if ($maxitems === 1) {
$whereUidMatched = $expr->eq('local.' . $field, $queryBuilder->quoteIdentifier('relation.uid'));
}

$queryBuilder
->select('relation.*')
->from($foreignTable, 'relation')
->join(
'relation',
$table,
'local',
(string)$expr->and($whereUidMatched, $expr->eq('local.uid', $uid))
);

if ($maxitems !== 1) {
$queryBuilder->getConcreteQueryBuilder()->orderBy($whereUidMatched);
}

if ($maxitems) {
$queryBuilder->setMaxResults($maxitems);
}

return $queryBuilder->executeQuery()->fetchAllAssociative();
}

/**
* @return list<array<string, string|int|float|bool>>
*/
Expand Down
Loading

0 comments on commit b5ac82c

Please sign in to comment.