22
33namespace InfyOm \Generator \Utils ;
44
5- use DB ;
65use Doctrine \DBAL \Schema \AbstractSchemaManager ;
76use Doctrine \DBAL \Schema \Column ;
7+ use Illuminate \Support \Facades \Schema ;
88use Illuminate \Support \Str ;
9+ use InfyOm \Generator \Common \GeneratorConfig ;
910use InfyOm \Generator \Common \GeneratorField ;
1011use InfyOm \Generator \Common \GeneratorFieldRelation ;
1112
@@ -59,45 +60,17 @@ class TableFieldsGenerator
5960 /** @var \Doctrine\DBAL\Schema\Table */
6061 public $ tableDetails ;
6162
63+ public GeneratorConfig $ config ;
64+
6265 public function __construct ($ tableName , $ ignoredFields , $ connection = '' )
6366 {
6467 $ this ->tableName = $ tableName ;
6568 $ this ->ignoredFields = $ ignoredFields ;
6669
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 );
9871 $ this ->columns = [];
9972 foreach ($ columns as $ column ) {
100- if (!in_array ($ column-> getName () , $ ignoredFields )) {
73+ if (!in_array ($ column[ ' name ' ] , $ ignoredFields )) {
10174 $ this ->columns [] = $ column ;
10275 }
10376 }
@@ -113,7 +86,7 @@ public function __construct($tableName, $ignoredFields, $connection = '')
11386 public function prepareFieldsFromTable ()
11487 {
11588 foreach ($ this ->columns as $ column ) {
116- $ type = $ column-> getType ()-> getName () ;
89+ $ type = $ column[ ' type_name ' ] ;
11790
11891 switch ($ type ) {
11992 case 'integer ' :
@@ -126,7 +99,7 @@ public function prepareFieldsFromTable()
12699 $ field = $ this ->generateIntFieldInput ($ column , 'bigInteger ' );
127100 break ;
128101 case 'boolean ' :
129- $ name = Str::title (str_replace ('_ ' , ' ' , $ column-> getName () ));
102+ $ name = Str::title (str_replace ('_ ' , ' ' , $ column[ ' name ' ] ));
130103 $ field = $ this ->generateField ($ column , 'boolean ' , 'checkbox ' );
131104 break ;
132105 case 'datetime ' :
@@ -166,8 +139,8 @@ public function prepareFieldsFromTable()
166139 $ field ->inIndex = false ;
167140 $ field ->inView = false ;
168141 }
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
171144
172145 $ this ->fields [] = $ field ;
173146 }
@@ -182,9 +155,14 @@ public function prepareFieldsFromTable()
182155 */
183156 public function getPrimaryKeyOfTable ($ tableName )
184157 {
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+ }
186164
187- return $ column ? $ column -> getColumns ()[ 0 ] : '' ;
165+ return $ column ;
188166 }
189167
190168 /**
@@ -216,17 +194,17 @@ public static function getTimestampFieldNames()
216194 private function generateIntFieldInput ($ column , $ dbType )
217195 {
218196 $ field = new GeneratorField ();
219- $ field ->name = $ column-> getName () ;
197+ $ field ->name = $ column[ ' name ' ] ;
220198 $ field ->parseDBType ($ dbType );
221199 $ field ->htmlType = 'number ' ;
222200
223- if ($ column-> getAutoincrement () ) {
201+ if ($ column[ ' auto_increment ' ] ) {
224202 $ field ->dbType .= ',true ' ;
225203 } else {
226204 $ field ->dbType .= ',false ' ;
227205 }
228206
229- if ($ column-> getUnsigned ( )) {
207+ if (str_contains ( $ column[ ' type ' ], ' unsigned ' )) {
230208 $ field ->dbType .= ',true ' ;
231209 }
232210
@@ -266,8 +244,8 @@ private function checkForPrimary(GeneratorField $field)
266244 private function generateField ($ column , $ dbType , $ htmlType )
267245 {
268246 $ 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 ;
271249 $ field ->parseDBType ($ dbType ); //, $column); TODO: handle column param
272250 $ field ->parseHtmlInput ($ htmlType );
273251
@@ -285,12 +263,13 @@ private function generateField($column, $dbType, $htmlType)
285263 private function generateNumberInput ($ column , $ dbType )
286264 {
287265 $ 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 ));
290269 $ field ->htmlType = 'number ' ;
291270
292271 if ($ dbType === 'decimal ' ) {
293- $ field ->numberDecimalPoints = $ column -> getScale () ;
272+ $ field ->numberDecimalPoints = explode ( ' , ' , $ length )[ 1 ] ;
294273 }
295274
296275 return $ this ->checkForPrimary ($ field );
@@ -312,25 +291,27 @@ public function prepareRelations()
312291 */
313292 public function prepareForeignKeys ()
314293 {
315- $ tables = $ this -> schemaManager -> listTables ();
294+ $ tables = Schema:: getTables ();
316295
317296 $ fields = [];
318297
319298 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+ }
323304 }
324305 $ formattedForeignKeys = [];
325- $ tableForeignKeys = $ table -> getForeignKeys ();
306+ $ tableForeignKeys = Schema:: getForeignKeys ($ table [ ' name ' ] );
326307 foreach ($ tableForeignKeys as $ tableForeignKey ) {
327308 $ 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 ' ] ;
334315
335316 $ formattedForeignKeys [] = $ generatorForeignKey ;
336317 }
@@ -339,7 +320,7 @@ public function prepareForeignKeys()
339320 $ generatorTable ->primaryKey = $ primaryKey ;
340321 $ generatorTable ->foreignKeys = $ formattedForeignKeys ;
341322
342- $ fields [$ table-> getName () ] = $ generatorTable ;
323+ $ fields [$ table[ ' name ' ] ] = $ generatorTable ;
343324 }
344325
345326 return $ fields ;
@@ -556,4 +537,4 @@ private function detectManyToOne($tables, $modelTable)
556537
557538 return $ manyToOneRelations ;
558539 }
559- }
540+ }
0 commit comments