22
33namespace N3XT0R \MigrationGenerator \Service \Generator \Normalization \Processors ;
44
5+ use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \FieldEntity ;
6+ use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \PrimaryKeyEntity ;
57use N3XT0R \MigrationGenerator \Service \Generator \Definition \Entity \ResultEntity ;
68use N3XT0R \MigrationGenerator \Service \Generator \Normalization \Context \NormalizationContext ;
79
@@ -12,42 +14,27 @@ public function process(NormalizationContext $context): ResultEntity
1214 $ result = $ context ->getCurrent ();
1315 $ results = $ result ->getResults ();
1416
15- foreach ($ results as $ tableName => $ definition ) {
16- $ primary = $ definition ['primary ' ] ?? [];
17-
18- if (is_array ($ primary ) && count ($ primary ) > 1 ) {
19- $ definition ['columns ' ] = [
20- 'id ' => [
21- 'type ' => 'int ' ,
22- 'primary ' => true ,
23- 'autoIncrement ' => true ,
24- ]
25- ] + $ definition ['columns ' ];
26-
27- foreach ($ primary as $ column ) {
28- if (isset ($ definition ['columns ' ][$ column ])) {
29- unset($ definition ['columns ' ][$ column ]['primary ' ]);
30- $ definition ['columns ' ][$ column ]['foreign ' ] = $ this ->guessForeignTarget ($ column );
31- }
32- }
33-
34- // 3. Neue primary definition
35- $ definition ['primary ' ] = ['id ' ];
36-
37- $ results [$ tableName ] = $ definition ;
38- dd ($ results );
39- $ result ->setResults ($ results );
17+ foreach ($ results as $ tableName => $ definitions ) {
18+ $ primary = current ($ definitions ['primaryKey ' ]);
19+ if ($ primary instanceof PrimaryKeyEntity && count ($ primary ->getColumns ()) >= 2 ) {
20+ $ idField = new FieldEntity ();
21+ $ idField ->setTable ($ tableName );
22+ $ idField ->setType ('bigInteger ' );
23+ $ idField ->setArguments (['autoIncrement ' => true ]);
24+ $ idField ->setOptions ([
25+ 'default ' => null ,
26+ 'unsigned ' => true ,
27+ 'nullable ' => false
28+ ]);
29+
30+ $ idField ->setColumnName ('id ' );
31+ $ results [$ tableName ]['table ' ] = ['id ' => $ idField , ...$ definitions ['table ' ]];
32+ unset($ results [$ tableName ]['primaryKey ' ]);
4033 }
4134 }
4235
36+ $ result ->setResults ($ results );
4337 $ context ->update ($ result );
4438 return $ result ;
4539 }
46-
47- private function guessForeignTarget (string $ column ): string
48- {
49- // z. B. user_id → users.id
50- $ name = rtrim ($ column , '_id ' ) . 's ' ;
51- return "$ name.id " ;
52- }
5340}
0 commit comments