2
2
3
3
namespace InfyOm \Generator \Utils ;
4
4
5
- use DB ;
6
5
use Doctrine \DBAL \Schema \AbstractSchemaManager ;
7
6
use Doctrine \DBAL \Schema \Column ;
7
+ use Illuminate \Support \Facades \Schema ;
8
8
use Illuminate \Support \Str ;
9
+ use InfyOm \Generator \Common \GeneratorConfig ;
9
10
use InfyOm \Generator \Common \GeneratorField ;
10
11
use InfyOm \Generator \Common \GeneratorFieldRelation ;
11
12
@@ -59,45 +60,17 @@ class TableFieldsGenerator
59
60
/** @var \Doctrine\DBAL\Schema\Table */
60
61
public $ tableDetails ;
61
62
63
+ public GeneratorConfig $ config ;
64
+
62
65
public function __construct ($ tableName , $ ignoredFields , $ connection = '' )
63
66
{
64
67
$ this ->tableName = $ tableName ;
65
68
$ this ->ignoredFields = $ ignoredFields ;
66
69
67
- $ connectionInstance = DB ::connection ($ connection ?: null );
68
-
69
- if (method_exists ($ connectionInstance , 'getDoctrineConnection ' )) {
70
- // Laravel 11+ preferred method
71
- $ this ->schemaManager = $ connectionInstance
72
- ->getDoctrineConnection ()
73
- ->createSchemaManager ();
74
- } else {
75
- // Laravel <11 fallback (optional)
76
- $ this ->schemaManager = $ connectionInstance ->getDoctrineSchemaManager ();
77
- }
78
-
79
- $ platform = $ this ->schemaManager ->getDatabasePlatform ();
80
- $ defaultMappings = [
81
- 'enum ' => 'string ' ,
82
- 'json ' => 'text ' ,
83
- 'bit ' => 'boolean ' ,
84
- ];
85
-
86
- // $this->tableDetails = $this->schemaManager->listTableDetails($this->tableName);
87
-
88
- $ mappings = config ('laravel_generator.from_table.doctrine_mappings ' , []);
89
- $ mappings = array_merge ($ mappings , $ defaultMappings );
90
- foreach ($ mappings as $ dbType => $ doctrineType ) {
91
- $ platform ->registerDoctrineTypeMapping ($ dbType , $ doctrineType );
92
- }
93
- // Added
94
- $ this ->tableDetails = $ this ->schemaManager ->listTableDetails ($ this ->tableName );
95
-
96
- $ columns = $ this ->schemaManager ->listTableColumns ($ tableName );
97
-
70
+ $ columns = Schema::getColumns ($ tableName );
98
71
$ this ->columns = [];
99
72
foreach ($ columns as $ column ) {
100
- if (!in_array ($ column-> getName () , $ ignoredFields )) {
73
+ if (!in_array ($ column[ ' name ' ] , $ ignoredFields )) {
101
74
$ this ->columns [] = $ column ;
102
75
}
103
76
}
@@ -113,7 +86,7 @@ public function __construct($tableName, $ignoredFields, $connection = '')
113
86
public function prepareFieldsFromTable ()
114
87
{
115
88
foreach ($ this ->columns as $ column ) {
116
- $ type = $ column-> getType ()-> getName () ;
89
+ $ type = $ column[ ' type_name ' ] ;
117
90
118
91
switch ($ type ) {
119
92
case 'integer ' :
@@ -126,7 +99,7 @@ public function prepareFieldsFromTable()
126
99
$ field = $ this ->generateIntFieldInput ($ column , 'bigInteger ' );
127
100
break ;
128
101
case 'boolean ' :
129
- $ name = Str::title (str_replace ('_ ' , ' ' , $ column-> getName () ));
102
+ $ name = Str::title (str_replace ('_ ' , ' ' , $ column[ ' name ' ] ));
130
103
$ field = $ this ->generateField ($ column , 'boolean ' , 'checkbox ' );
131
104
break ;
132
105
case 'datetime ' :
@@ -166,8 +139,8 @@ public function prepareFieldsFromTable()
166
139
$ field ->inIndex = false ;
167
140
$ field ->inView = false ;
168
141
}
169
- $ field ->isNotNull = $ column-> getNotNull () ;
170
- $ field ->description = $ column-> getComment () ?? '' ; // get comments from table
142
+ $ field ->isNotNull = ! $ column[ ' nullable ' ] ;
143
+ $ field ->description = $ column[ ' comment ' ] ?? '' ; // get comments from table
171
144
172
145
$ this ->fields [] = $ field ;
173
146
}
@@ -182,9 +155,14 @@ public function prepareFieldsFromTable()
182
155
*/
183
156
public function getPrimaryKeyOfTable ($ tableName )
184
157
{
185
- $ column = $ this ->schemaManager ->listTableDetails ($ tableName )->getPrimaryKey ();
158
+ $ column = '' ;
159
+ foreach (Schema::getIndexes ($ tableName ) as $ index ) {
160
+ if ($ index ['primary ' ]) {
161
+ $ column = $ index ['columns ' ][0 ];
162
+ }
163
+ }
186
164
187
- return $ column ? $ column -> getColumns ()[ 0 ] : '' ;
165
+ return $ column ;
188
166
}
189
167
190
168
/**
@@ -216,17 +194,17 @@ public static function getTimestampFieldNames()
216
194
private function generateIntFieldInput ($ column , $ dbType )
217
195
{
218
196
$ field = new GeneratorField ();
219
- $ field ->name = $ column-> getName () ;
197
+ $ field ->name = $ column[ ' name ' ] ;
220
198
$ field ->parseDBType ($ dbType );
221
199
$ field ->htmlType = 'number ' ;
222
200
223
- if ($ column-> getAutoincrement () ) {
201
+ if ($ column[ ' auto_increment ' ] ) {
224
202
$ field ->dbType .= ',true ' ;
225
203
} else {
226
204
$ field ->dbType .= ',false ' ;
227
205
}
228
206
229
- if ($ column-> getUnsigned ( )) {
207
+ if (str_contains ( $ column[ ' type ' ], ' unsigned ' )) {
230
208
$ field ->dbType .= ',true ' ;
231
209
}
232
210
@@ -266,8 +244,8 @@ private function checkForPrimary(GeneratorField $field)
266
244
private function generateField ($ column , $ dbType , $ htmlType )
267
245
{
268
246
$ field = new GeneratorField ();
269
- $ field ->name = $ column-> getName () ;
270
- $ field ->fieldDetails = $ this -> tableDetails -> getColumn ( $ field -> name ) ;
247
+ $ field ->name = $ column[ ' name ' ] ;
248
+ $ field ->fieldDetails = $ column ;
271
249
$ field ->parseDBType ($ dbType ); //, $column); TODO: handle column param
272
250
$ field ->parseHtmlInput ($ htmlType );
273
251
@@ -285,12 +263,13 @@ private function generateField($column, $dbType, $htmlType)
285
263
private function generateNumberInput ($ column , $ dbType )
286
264
{
287
265
$ field = new GeneratorField ();
288
- $ field ->name = $ column ->getName ();
289
- $ field ->parseDBType ($ dbType .', ' .$ column ->getPrecision ().', ' .$ column ->getScale ());
266
+ $ field ->name = $ column ['name ' ];
267
+ $ length = get_field_length ($ column ['type ' ]);
268
+ $ field ->parseDBType ($ dbType .', ' .get_field_precision ($ length ).', ' .get_field_scale ($ length ));
290
269
$ field ->htmlType = 'number ' ;
291
270
292
271
if ($ dbType === 'decimal ' ) {
293
- $ field ->numberDecimalPoints = $ column -> getScale () ;
272
+ $ field ->numberDecimalPoints = explode ( ' , ' , $ length )[ 1 ] ;
294
273
}
295
274
296
275
return $ this ->checkForPrimary ($ field );
@@ -312,25 +291,27 @@ public function prepareRelations()
312
291
*/
313
292
public function prepareForeignKeys ()
314
293
{
315
- $ tables = $ this -> schemaManager -> listTables ();
294
+ $ tables = Schema:: getTables ();
316
295
317
296
$ fields = [];
318
297
319
298
foreach ($ tables as $ table ) {
320
- $ primaryKey = $ table ->getPrimaryKey ();
321
- if ($ primaryKey ) {
322
- $ primaryKey = $ primaryKey ->getColumns ()[0 ];
299
+ $ primaryKey = '' ;
300
+ foreach (Schema::getIndexes ($ table ['name ' ]) as $ index ) {
301
+ if ($ index ['primary ' ]) {
302
+ $ primaryKey = $ index ['columns ' ][0 ];
303
+ }
323
304
}
324
305
$ formattedForeignKeys = [];
325
- $ tableForeignKeys = $ table -> getForeignKeys ();
306
+ $ tableForeignKeys = Schema:: getForeignKeys ($ table [ ' name ' ] );
326
307
foreach ($ tableForeignKeys as $ tableForeignKey ) {
327
308
$ generatorForeignKey = new GeneratorForeignKey ();
328
- $ generatorForeignKey ->name = $ tableForeignKey-> getName () ;
329
- $ generatorForeignKey ->localField = $ tableForeignKey-> getLocalColumns () [0 ];
330
- $ generatorForeignKey ->foreignField = $ tableForeignKey-> getForeignColumns () [0 ];
331
- $ generatorForeignKey ->foreignTable = $ tableForeignKey-> getForeignTableName () ;
332
- $ generatorForeignKey ->onUpdate = $ tableForeignKey-> onUpdate () ;
333
- $ generatorForeignKey ->onDelete = $ tableForeignKey-> onDelete () ;
309
+ $ generatorForeignKey ->name = $ tableForeignKey[ ' name ' ] ;
310
+ $ generatorForeignKey ->localField = $ tableForeignKey[ ' columns ' ] [0 ];
311
+ $ generatorForeignKey ->foreignField = $ tableForeignKey[ ' foreign_columns ' ] [0 ];
312
+ $ generatorForeignKey ->foreignTable = $ tableForeignKey[ ' foreign_table ' ] ;
313
+ $ generatorForeignKey ->onUpdate = $ tableForeignKey[ ' on_update ' ] ;
314
+ $ generatorForeignKey ->onDelete = $ tableForeignKey[ ' on_delete ' ] ;
334
315
335
316
$ formattedForeignKeys [] = $ generatorForeignKey ;
336
317
}
@@ -339,7 +320,7 @@ public function prepareForeignKeys()
339
320
$ generatorTable ->primaryKey = $ primaryKey ;
340
321
$ generatorTable ->foreignKeys = $ formattedForeignKeys ;
341
322
342
- $ fields [$ table-> getName () ] = $ generatorTable ;
323
+ $ fields [$ table[ ' name ' ] ] = $ generatorTable ;
343
324
}
344
325
345
326
return $ fields ;
@@ -556,4 +537,4 @@ private function detectManyToOne($tables, $modelTable)
556
537
557
538
return $ manyToOneRelations ;
558
539
}
559
- }
540
+ }
0 commit comments