Skip to content

Commit 15a8c31

Browse files
committed
[BUGFIX] Build indexes with foreign_field and foreign_table_field
Fixes: #398
1 parent 34a96bb commit 15a8c31

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

Build/content-blocks-examples/ContentBlocks/ContentElements/tabs/config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@ fields:
66
type: Collection
77
minitems: 1
88
foreign_table: tt_content
9+
shareAcrossFields: true
10+
shareAcrossTables: true
11+
allowedRecordTypes:
12+
- example_text
13+
- identifier: tabs_item2
14+
type: Collection
15+
minitems: 1
16+
foreign_table: tt_content
17+
shareAcrossFields: true
18+
shareAcrossTables: true
919
allowedRecordTypes:
1020
- example_text

Classes/Generator/SqlGenerator.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,33 @@ public function generate(): array
7171
protected function handleParentReferences(TableDefinition $tableDefinition): array
7272
{
7373
$sql = [];
74+
$indexes = [];
7475
foreach ($tableDefinition->parentReferences as $parentReference) {
7576
$parentTcaConfig = $parentReference->getTca()['config'];
7677
// Generate indexes for the parent uid field for better performance.
7778
if (isset($parentTcaConfig['foreign_field'])) {
7879
$foreignField = $parentTcaConfig['foreign_field'];
79-
$sqlStatement = 'CREATE TABLE `' . $tableDefinition->table . '` (KEY parent_uid (' . $foreignField . '));';
80-
if (!in_array($sqlStatement, $sql, true)) {
81-
$sql[] = $sqlStatement;
82-
}
80+
$indexes[$tableDefinition->table][] = $foreignField;
81+
}
82+
if (isset($parentTcaConfig['foreign_table_field'])) {
83+
$foreignTableName = $parentTcaConfig['foreign_table_field'];
84+
$indexes[$tableDefinition->table][] = $foreignTableName;
8385
}
8486
// The foreign_match_fields fields are automatically added, so that feature "shareAcrossFields" works.
8587
foreach ($parentTcaConfig['foreign_match_fields'] ?? [] as $foreignMatchField => $foreignMatchValue) {
88+
$indexes[$tableDefinition->table][] = $foreignMatchField;
8689
$sqlStatement = 'CREATE TABLE `' . $tableDefinition->table . '` (`' . $foreignMatchField . '` varchar(255) DEFAULT \'\' NOT NULL);';
8790
if (!in_array($sqlStatement, $sql, true)) {
8891
$sql[] = $sqlStatement;
8992
}
9093
}
9194
}
95+
foreach ($indexes as $table => $indexFields) {
96+
$sqlStatement = 'CREATE TABLE `' . $table . '` (KEY parent_uid (' . implode(', ', $indexFields) . '));';
97+
if (!in_array($sqlStatement, $sql, true)) {
98+
$sql[] = $sqlStatement;
99+
}
100+
}
92101
return $sql;
93102
}
94103
}

Tests/Unit/Generator/SqlGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public static function generateReturnsExpectedSqlStatementsDataProvider(): itera
7373
],
7474
],
7575
'expected' => [
76-
'CREATE TABLE `foobar` (KEY parent_uid (foreign_table_parent_uid));',
7776
"CREATE TABLE `foobar` (`fieldname` varchar(255) DEFAULT '' NOT NULL);",
77+
'CREATE TABLE `foobar` (KEY parent_uid (foreign_table_parent_uid, tablenames, fieldname));',
7878
],
7979
];
8080

0 commit comments

Comments
 (0)