@@ -22,33 +22,63 @@ public function process(NormalizationContext $context): ResultEntity
2222 $ results = $ result ->getResults ();
2323
2424 foreach ($ results as $ tableName => $ definitions ) {
25+ if (!isset ($ definitions ['primaryKey ' ])) {
26+ continue ;
27+ }
28+
29+ /** @var PrimaryKeyEntity|null $primary */
2530 $ primary = current ($ definitions ['primaryKey ' ]);
26- $ pkColumns = $ primary ->getColumns ();
27- if ($ primary instanceof PrimaryKeyEntity && count ($ pkColumns ) >= 2 ) {
28- $ idField = new FieldEntity ();
29- $ idField ->setTable ($ tableName );
30- $ idField ->setType ('bigInteger ' );
31- $ idField ->setArguments (['autoIncrement ' => true ]);
32- $ idField ->setOptions ([
33- 'default ' => null ,
34- 'unsigned ' => true ,
35- 'nullable ' => false
36- ]);
37-
38- $ uniqueIndex = new IndexEntity ();
39- $ uniqueIndex ->setType ('unique ' );
40- $ uniqueIndex ->setColumns ($ pkColumns );
41-
42- $ idField ->setColumnName ('id ' );
43- $ results [$ tableName ]['table ' ] = ['id ' => $ idField , ...$ definitions ['table ' ]];
44- $ results [$ tableName ]['index ' ] = [$ uniqueIndex , ...$ definitions ['index ' ]];
45- unset($ results [$ tableName ]['primaryKey ' ]);
46- //dump($results);
31+ if (!$ this ->isCompositePrimaryKey ($ primary )) {
32+ continue ;
4733 }
34+
35+ $ results [$ tableName ] = $ this ->transformPivotTable ($ tableName , $ definitions , $ primary );
4836 }
4937
5038 $ result ->setResults ($ results );
5139 $ context ->update ($ result );
40+
5241 return $ result ;
5342 }
43+
44+ protected function isCompositePrimaryKey (?PrimaryKeyEntity $ primary ): bool
45+ {
46+ return $ primary instanceof PrimaryKeyEntity && count ($ primary ->getColumns ()) >= 2 ;
47+ }
48+
49+ protected function transformPivotTable (string $ tableName , array $ definitions , PrimaryKeyEntity $ primary ): array
50+ {
51+ $ definitions ['table ' ] = $ this ->prependIdField ($ tableName , $ definitions ['table ' ]);
52+ unset($ definitions ['primaryKey ' ]);
53+ $ definitions ['index ' ][] = $ this ->createUniqueIndexFromPrimaryKey ($ primary );
54+
55+ return $ definitions ;
56+ }
57+
58+ protected function prependIdField (string $ tableName , array $ fields ): array
59+ {
60+ $ idField = new FieldEntity ();
61+ $ idField ->setTable ($ tableName );
62+ $ idField ->setType ('bigInteger ' );
63+ $ idField ->setArguments (['autoIncrement ' => true ]);
64+ $ idField ->setOptions ([
65+ 'default ' => null ,
66+ 'unsigned ' => true ,
67+ 'nullable ' => false ,
68+ ]);
69+ $ idField ->setColumnName ('id ' );
70+
71+ return ['id ' => $ idField ] + $ fields ;
72+ }
73+
74+ protected function createUniqueIndexFromPrimaryKey (PrimaryKeyEntity $ primary ): IndexEntity
75+ {
76+ $ index = new IndexEntity ();
77+ $ index ->setType ('unique ' );
78+ $ index ->setIndexType ('index ' );
79+ $ index ->setColumns ($ primary ->getColumns ());
80+ $ index ->setName (implode ('_ ' , $ primary ->getColumns ()) . '_unique ' );
81+
82+ return $ index ;
83+ }
5484}
0 commit comments