/
DefaultTcaSchema.php
864 lines (804 loc) · 35.2 KB
/
DefaultTcaSchema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Database\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
use TYPO3\CMS\Core\Database\Schema\Exception\DefaultTcaSchemaTablePositionException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class is called by the SchemaMigrator after all extension's ext_tables.sql
* files have been parsed and processed to the doctrine Table/Column/Index objects.
*
* Method enrich() goes through all $GLOBALS['TCA'] tables and adds fields like
* 'uid', 'sorting', 'deleted' and friends if the feature is enabled in TCA and the
* field has not been defined in ext_tables.sql files.
*
* This allows extension developers to leave out the TYPO3 DB management fields
* and reduce ext_tables.sql of extensions down to the business fields.
*
* @internal
*/
class DefaultTcaSchema
{
/**
* Add fields to $tables array that has been created from ext_tables.sql files.
* This goes through all tables defined in TCA, looks for 'ctrl' features like
* "soft delete" ['ctrl']['delete'] and adds the field if it has not been
* defined in ext_tables.sql, yet.
*
* @param Table[] $tables
* @return Table[] Modified tables
*/
public function enrich(array $tables): array
{
$tables = $this->enrichSingleTableFields($tables);
$tables = $this->enrichMmTables($tables);
return $tables;
}
/**
* Add single fields of TCA tables like uid, sorting and similar.
*/
protected function enrichSingleTableFields($tables)
{
foreach ($GLOBALS['TCA'] as $tableName => $tableDefinition) {
$isTableDefined = $this->isTableDefined($tables, $tableName);
if (!$isTableDefined) {
continue;
}
// If the table is given in existing $tables list, add all fields to the first
// position of that table - in case it is in there multiple times which happens
// if extensions add single fields to tables that have been defined in
// other ext_tables.sql, too.
$tablePosition = $this->getTableFirstPosition($tables, $tableName);
// uid column and primary key if uid is not defined
if (!$this->isColumnDefinedForTable($tables, $tableName, 'uid')) {
$tables[$tablePosition]->addColumn(
$this->quote('uid'),
Types::INTEGER,
[
'notnull' => true,
'unsigned' => true,
'autoincrement' => true,
]
);
$tables[$tablePosition]->setPrimaryKey(['uid']);
}
// pid column and prepare parent key if pid is not defined
$pidColumnAdded = false;
if (!$this->isColumnDefinedForTable($tables, $tableName, 'pid')) {
$options = [
'default' => 0,
'notnull' => true,
'unsigned' => true,
];
$tables[$tablePosition]->addColumn($this->quote('pid'), Types::INTEGER, $options);
$pidColumnAdded = true;
}
// tstamp column
if (!empty($tableDefinition['ctrl']['tstamp'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['tstamp'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['tstamp']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// crdate column
if (!empty($tableDefinition['ctrl']['crdate'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['crdate'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['crdate']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// deleted column - soft delete
if (!empty($tableDefinition['ctrl']['delete'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['delete'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['delete']),
Types::SMALLINT,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// disabled column
if (!empty($tableDefinition['ctrl']['enablecolumns']['disabled'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['enablecolumns']['disabled'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['enablecolumns']['disabled']),
Types::SMALLINT,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// starttime column
if (!empty($tableDefinition['ctrl']['enablecolumns']['starttime'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['enablecolumns']['starttime'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['enablecolumns']['starttime']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// endtime column
if (!empty($tableDefinition['ctrl']['enablecolumns']['endtime'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['enablecolumns']['endtime'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['enablecolumns']['endtime']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// fe_group column
if (!empty($tableDefinition['ctrl']['enablecolumns']['fe_group'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['enablecolumns']['fe_group'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['enablecolumns']['fe_group']),
Types::STRING,
[
'default' => '0',
'notnull' => true,
'length' => 255,
]
);
}
// sorting column
if (!empty($tableDefinition['ctrl']['sortby'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['sortby'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['sortby']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => false,
]
);
}
// index on pid column and maybe others - only if pid has not been defined via ext_tables.sql before
if ($pidColumnAdded && !$this->isIndexDefinedForTable($tables, $tableName, 'parent')) {
$parentIndexFields = ['pid'];
if (!empty($tableDefinition['ctrl']['delete'])) {
$parentIndexFields[] = (string)$tableDefinition['ctrl']['delete'];
}
if (!empty($tableDefinition['ctrl']['enablecolumns']['disabled'])) {
$parentIndexFields[] = (string)$tableDefinition['ctrl']['enablecolumns']['disabled'];
}
$tables[$tablePosition]->addIndex($parentIndexFields, 'parent');
}
// description column
if (!empty($tableDefinition['ctrl']['descriptionColumn'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['descriptionColumn'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['descriptionColumn']),
Types::TEXT,
[
'notnull' => false,
'length' => 65535,
]
);
}
// editlock column
if (!empty($tableDefinition['ctrl']['editlock'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['editlock'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['editlock']),
Types::SMALLINT,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// sys_language_uid column
if (!empty($tableDefinition['ctrl']['languageField'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['languageField'])
) {
$tables[$tablePosition]->addColumn(
$this->quote((string)$tableDefinition['ctrl']['languageField']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => false,
]
);
}
// l10n_parent column
if (!empty($tableDefinition['ctrl']['languageField'])
&& !empty($tableDefinition['ctrl']['transOrigPointerField'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['transOrigPointerField'])
) {
$tables[$tablePosition]->addColumn(
$this->quote((string)$tableDefinition['ctrl']['transOrigPointerField']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// l10n_source column
if (!empty($tableDefinition['ctrl']['languageField'])
&& !empty($tableDefinition['ctrl']['translationSource'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['translationSource'])
) {
$tables[$tablePosition]->addColumn(
$this->quote((string)$tableDefinition['ctrl']['translationSource']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
$tables[$tablePosition]->addIndex([$tableDefinition['ctrl']['translationSource']], 'translation_source');
}
// l10n_state column
if (!empty($tableDefinition['ctrl']['languageField'])
&& !empty($tableDefinition['ctrl']['transOrigPointerField'])
&& !$this->isColumnDefinedForTable($tables, $tableName, 'l10n_state')
) {
$tables[$tablePosition]->addColumn(
$this->quote('l10n_state'),
Types::TEXT,
[
'notnull' => false,
'length' => 65535,
]
);
}
// t3_origuid column
if (!empty($tableDefinition['ctrl']['origUid'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['origUid'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['origUid']),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// l18n_diffsource column
if (!empty($tableDefinition['ctrl']['transOrigDiffSourceField'])
&& !$this->isColumnDefinedForTable($tables, $tableName, $tableDefinition['ctrl']['transOrigDiffSourceField'])
) {
$tables[$tablePosition]->addColumn(
$this->quote($tableDefinition['ctrl']['transOrigDiffSourceField']),
Types::BLOB,
[
// mediumblob (16MB) on mysql
'length' => 16777215,
'notnull' => false,
]
);
}
// workspaces t3ver_oid column
if (!empty($tableDefinition['ctrl']['versioningWS'])
&& (bool)$tableDefinition['ctrl']['versioningWS'] === true
&& !$this->isColumnDefinedForTable($tables, $tableName, 't3ver_oid')
) {
$tables[$tablePosition]->addColumn(
$this->quote('t3ver_oid'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// workspaces t3ver_wsid column
if (!empty($tableDefinition['ctrl']['versioningWS'])
&& (bool)$tableDefinition['ctrl']['versioningWS'] === true
&& !$this->isColumnDefinedForTable($tables, $tableName, 't3ver_wsid')
) {
$tables[$tablePosition]->addColumn(
$this->quote('t3ver_wsid'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
// workspaces t3ver_state column
if (!empty($tableDefinition['ctrl']['versioningWS'])
&& (bool)$tableDefinition['ctrl']['versioningWS'] === true
&& !$this->isColumnDefinedForTable($tables, $tableName, 't3ver_state')
) {
$tables[$tablePosition]->addColumn(
$this->quote('t3ver_state'),
Types::SMALLINT,
[
'default' => 0,
'notnull' => true,
'unsigned' => false,
]
);
}
// workspaces t3ver_stage column
if (!empty($tableDefinition['ctrl']['versioningWS'])
&& (bool)$tableDefinition['ctrl']['versioningWS'] === true
&& !$this->isColumnDefinedForTable($tables, $tableName, 't3ver_stage')
) {
$tables[$tablePosition]->addColumn(
$this->quote('t3ver_stage'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => false,
]
);
}
// workspaces index on t3ver_oid and t3ver_wsid fields
if (!empty($tableDefinition['ctrl']['versioningWS'])
&& (bool)$tableDefinition['ctrl']['versioningWS'] === true
&& !$this->isIndexDefinedForTable($tables, $tableName, 't3ver_oid')
) {
$tables[$tablePosition]->addIndex(['t3ver_oid', 't3ver_wsid'], 't3ver_oid');
}
// In the following, columns for TCA fields with a dedicated TCA type are
// added. In the unlikely case that no columns exist, we can skip the table.
if (!isset($tableDefinition['columns']) || !is_array($tableDefinition['columns'])) {
continue;
}
// Add category fields for all tables, defining category columns (TCA type=category)
foreach ($tableDefinition['columns'] as $fieldName => $fieldConfig) {
if ((string)($fieldConfig['config']['type'] ?? '') !== 'category'
|| $this->isColumnDefinedForTable($tables, $tableName, $fieldName)
) {
continue;
}
if (($fieldConfig['config']['relationship'] ?? '') === 'oneToMany') {
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
Types::TEXT,
[
'notnull' => false,
]
);
} else {
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
}
// Add datetime fields for all tables, defining datetime columns (TCA type=datetime), except
// those columns, which had already been added due to definition in "ctrl", e.g. "starttime".
foreach ($tableDefinition['columns'] as $fieldName => $fieldConfig) {
if ((string)($fieldConfig['config']['type'] ?? '') !== 'datetime'
|| $this->isColumnDefinedForTable($tables, $tableName, $fieldName)
) {
continue;
}
if (in_array($fieldConfig['config']['dbType'] ?? '', QueryHelper::getDateTimeTypes(), true)) {
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
$fieldConfig['config']['dbType'],
[
'notnull' => false,
]
);
} else {
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
Types::INTEGER,
[
'default' => 0,
'notnull' => !($fieldConfig['config']['nullable'] ?? false),
'unsigned' => false,
]
);
}
}
// Add slug fields for all tables, defining slug columns (TCA type=slug)
foreach ($tableDefinition['columns'] as $fieldName => $fieldConfig) {
if ((string)($fieldConfig['config']['type'] ?? '') !== 'slug'
|| $this->isColumnDefinedForTable($tables, $tableName, $fieldName)
) {
continue;
}
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
Types::STRING,
[
'length' => 2048,
'notnull' => false,
]
);
}
// Add json fields for all tables, defining json columns (TCA type=json)
foreach ($tableDefinition['columns'] as $fieldName => $fieldConfig) {
if ((string)($fieldConfig['config']['type'] ?? '') !== 'json'
|| $this->isColumnDefinedForTable($tables, $tableName, $fieldName)
) {
continue;
}
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
Types::JSON,
[
'notnull' => false,
]
);
}
// Add uuid fields for all tables, defining uuid columns (TCA type=uuid)
foreach ($tableDefinition['columns'] as $fieldName => $fieldConfig) {
if ((string)($fieldConfig['config']['type'] ?? '') !== 'uuid'
|| $this->isColumnDefinedForTable($tables, $tableName, $fieldName)
) {
continue;
}
$tables[$tablePosition]->addColumn(
$this->quote($fieldName),
Types::STRING,
[
'length' => 36,
'default' => '',
'notnull' => true,
]
);
}
}
return $tables;
}
/**
* Find table fields that configure a "true" MM relation and define the
* according mm table schema for them. True MM tables are intermediate tables
* that have NO TCA itself. Those are indicated by type=select and type=group
* and type=inline fields with MM property.
*/
protected function enrichMmTables($tables): array
{
foreach ($GLOBALS['TCA'] as $tableName => $tableDefinition) {
// Consider this TCA table only if it is within the set of incoming tables. Important
// when the schema analyzer is used within extension manager for a sub set of tables.
$isTableDefined = $this->isTableDefined($tables, $tableName);
if (!$isTableDefined) {
continue;
}
if (!is_array($tableDefinition['columns'] ?? false)) {
// TCA definition in general is broken if there are no specified columns. Skip to be sure here.
continue;
}
foreach ($tableDefinition['columns'] as $tcaColumn) {
if (
!is_array($tcaColumn['config'] ?? false)
|| !is_string($tcaColumn['config']['type'] ?? false)
|| !in_array($tcaColumn['config']['type'], ['select', 'group', 'inline', 'category'], true)
|| !is_string($tcaColumn['config']['MM'] ?? false)
// Consider this mm only if looking at it from the local side
|| ($tcaColumn['config']['MM_opposite_field'] ?? false)
) {
// Broken TCA or not of expected type, or no MM, or foreign side
continue;
}
$mmTableName = $tcaColumn['config']['MM'];
try {
// If the mm table is defined, work with it. Else add at and.
$tablePosition = $this->getTableFirstPosition($tables, $mmTableName);
} catch (DefaultTcaSchemaTablePositionException $e) {
$tablePosition = array_key_last($tables) + 1;
$tables[$tablePosition] = GeneralUtility::makeInstance(
Table::class,
$mmTableName
);
}
// Add 'uid' field with primary key if MM_hasUidField is set.
// @todo: ['config']['MM_hasUidField'] is only (?!) needed when ['config']['multiple'] = true. It seems
// as if we could drop TCA MM_hasUidField and simply test for 'multiple' to simplify things.
$hasUid = (bool)($tcaColumn['config']['MM_hasUidField'] ?? false);
if ($hasUid && !$this->isColumnDefinedForTable($tables, $mmTableName, 'uid')) {
$tables[$tablePosition]->addColumn(
$this->quote('uid'),
Types::INTEGER,
[
'notnull' => true,
'unsigned' => true,
'autoincrement' => true,
]
);
$tables[$tablePosition]->setPrimaryKey(['uid']);
}
if (!$this->isColumnDefinedForTable($tables, $mmTableName, 'uid_local')) {
$tables[$tablePosition]->addColumn(
$this->quote('uid_local'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
if (!$this->isIndexDefinedForTable($tables, $mmTableName, 'uid_local')) {
$tables[$tablePosition]->addIndex(['uid_local'], 'uid_local');
}
if (!$this->isColumnDefinedForTable($tables, $mmTableName, 'uid_foreign')) {
$tables[$tablePosition]->addColumn(
$this->quote('uid_foreign'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
if (!$this->isIndexDefinedForTable($tables, $mmTableName, 'uid_foreign')) {
$tables[$tablePosition]->addIndex(['uid_foreign'], 'uid_foreign');
}
if (!$this->isColumnDefinedForTable($tables, $mmTableName, 'sorting')) {
$tables[$tablePosition]->addColumn(
$this->quote('sorting'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
if (!$this->isColumnDefinedForTable($tables, $mmTableName, 'sorting_foreign')) {
$tables[$tablePosition]->addColumn(
$this->quote('sorting_foreign'),
Types::INTEGER,
[
'default' => 0,
'notnull' => true,
'unsigned' => true,
]
);
}
if (!empty($tcaColumn['config']['MM_oppositeUsage'])) {
// This local table can be the target of multiple foreign tables and table fields. The mm table
// thus needs two further fields to specify which foreign/table field combination links is used.
// Those are stored in two additional fields called "tablenames" and "fieldname".
if (!$this->isColumnDefinedForTable($tables, $mmTableName, 'tablenames')) {
$tables[$tablePosition]->addColumn(
$this->quote('tablenames'),
Types::STRING,
[
'default' => '',
'length' => 64,
'notnull' => true,
]
);
}
if (!$this->isColumnDefinedForTable($tables, $mmTableName, 'fieldname')) {
$tables[$tablePosition]->addColumn(
$this->quote('fieldname'),
Types::STRING,
[
'default' => '',
'length' => 64,
'notnull' => true,
]
);
}
}
// Primary key handling: If there is a uid field, PK has been added above already.
// Otherwise, the PK combination is either "uid_local, uid_foreign", or
// "uid_local, uid_foreign, tablenames, fieldname" if this is a multi-foreign setup.
if (!$hasUid && $tables[$tablePosition]->getPrimaryKey() === null && !empty($tcaColumn['config']['MM_oppositeUsage'])) {
$tables[$tablePosition]->setPrimaryKey(['uid_local', 'uid_foreign', 'tablenames', 'fieldname']);
} elseif (!$hasUid && $tables[$tablePosition]->getPrimaryKey() === null) {
$tables[$tablePosition]->setPrimaryKey(['uid_local', 'uid_foreign']);
}
}
}
return $tables;
}
/**
* If the enrich() method adds fields, they should be added in the beginning of a table.
* This has is done for cosmetically reasons to improve readability of db schema when
* opening tables in a database browser.
*
* @return string[]
*/
public function getPrioritizedFieldNames(string $tableName): array
{
if (!isset($GLOBALS['TCA'][$tableName]['ctrl'])) {
return [];
}
$prioritizedFieldNames = [
'uid',
'pid',
];
$tableDefinition = $GLOBALS['TCA'][$tableName]['ctrl'];
if (!empty($tableDefinition['crdate'])) {
$prioritizedFieldNames[] = $tableDefinition['crdate'];
}
if (!empty($tableDefinition['tstamp'])) {
$prioritizedFieldNames[] = $tableDefinition['tstamp'];
}
if (!empty($tableDefinition['delete'])) {
$prioritizedFieldNames[] = $tableDefinition['delete'];
}
if (!empty($tableDefinition['enablecolumns']['disabled'])) {
$prioritizedFieldNames[] = $tableDefinition['enablecolumns']['disabled'];
}
if (!empty($tableDefinition['enablecolumns']['starttime'])) {
$prioritizedFieldNames[] = $tableDefinition['enablecolumns']['starttime'];
}
if (!empty($tableDefinition['enablecolumns']['endtime'])) {
$prioritizedFieldNames[] = $tableDefinition['enablecolumns']['endtime'];
}
if (!empty($tableDefinition['enablecolumns']['fe_group'])) {
$prioritizedFieldNames[] = $tableDefinition['enablecolumns']['fe_group'];
}
if (!empty($tableDefinition['languageField'])) {
$prioritizedFieldNames[] = $tableDefinition['languageField'];
if (!empty($tableDefinition['transOrigPointerField'])) {
$prioritizedFieldNames[] = $tableDefinition['transOrigPointerField'];
$prioritizedFieldNames[] = 'l10n_state';
}
if (!empty($tableDefinition['translationSource'])) {
$prioritizedFieldNames[] = $tableDefinition['translationSource'];
}
if (!empty($tableDefinition['transOrigDiffSourceField'])) {
$prioritizedFieldNames[] = $tableDefinition['transOrigDiffSourceField'];
}
}
if (!empty($tableDefinition['sortby'])) {
$prioritizedFieldNames[] = $tableDefinition['sortby'];
}
if (!empty($tableDefinition['descriptionColumn'])) {
$prioritizedFieldNames[] = $tableDefinition['descriptionColumn'];
}
if (!empty($tableDefinition['editlock'])) {
$prioritizedFieldNames[] = $tableDefinition['editlock'];
}
if (!empty($tableDefinition['origUid'])) {
$prioritizedFieldNames[] = $tableDefinition['origUid'];
}
if (!empty($tableDefinition['versioningWS'])) {
$prioritizedFieldNames[] = 't3ver_wsid';
$prioritizedFieldNames[] = 't3ver_oid';
$prioritizedFieldNames[] = 't3ver_state';
$prioritizedFieldNames[] = 't3ver_stage';
}
return $prioritizedFieldNames;
}
/**
* True if table with given table name is defined within incoming $tables array
*
* @param Table[] $tables
*/
protected function isTableDefined(array $tables, string $tableName): bool
{
foreach ($tables as $table) {
if ($table->getName() === $tableName) {
return true;
}
}
return false;
}
/**
* True if a column with a given name is defined within the incoming
* array of Table's.
*
* @param Table[] $tables
*/
protected function isColumnDefinedForTable(array $tables, string $tableName, string $fieldName): bool
{
foreach ($tables as $table) {
if ($table->getName() !== $tableName) {
continue;
}
$columns = $table->getColumns();
foreach ($columns as $column) {
if ($column->getName() === $fieldName) {
return true;
}
}
}
return false;
}
/**
* True if an index with a given name is defined within the incoming
* array of Table's.
*
* @param Table[] $tables
*/
protected function isIndexDefinedForTable(array $tables, string $tableName, string $indexName): bool
{
foreach ($tables as $table) {
if ($table->getName() !== $tableName) {
continue;
}
$indexes = $table->getIndexes();
foreach ($indexes as $index) {
if ($index->getName() === $indexName) {
return true;
}
}
}
return false;
}
/**
* The incoming $tables array can contain Table objects for the same table
* multiple times. This can happen if an extension has the main CREATE TABLE
* statement in its ext_tables.sql and another extension adds or changes further
* fields in an own CREATE TABLE statement.
*
* @todo It would be better if the incoming $tables structure would be cleaned
* @todo to contain a table only once before this class is entered.
*
* @param Table[] $tables
* @throws \RuntimeException
*/
protected function getTableFirstPosition(array $tables, string $tableName): int
{
foreach ($tables as $position => $table) {
if ($table->getName() === $tableName) {
return (int)$position;
}
}
throw new DefaultTcaSchemaTablePositionException('Table ' . $tableName . ' not found in schema list', 1527854474);
}
protected function quote(string $identifier): string
{
return '`' . $identifier . '`';
}
}