Skip to content

Commit 50c0c2c

Browse files
authored
Merge pull request #44 from qnnp-me/main
fix(commands): 修复 MakeModelCommand 中表名和字段信息生成的逻辑问题
2 parents 48dc60b + 031eac8 commit 50c0c2c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/Commands/MakeModelCommand.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,16 @@ protected function createModel($class, $namespace, $file, $connection = null)
122122
$inflector = InflectorFactory::create()->build();
123123
$table_plura = $inflector->pluralize($inflector->tableize($class));
124124
$con = Db::connection($connection);
125-
125+
126126
// 检查表是否存在(兼容MySQL和PostgreSQL)
127127
if ($driver === 'pgsql') {
128128
// PostgreSQL 表检查
129129
$schema = config("database.connections.$connection.schema") ?? 'public';
130130
$exists_plura = $con->select("SELECT to_regclass('{$schema}.{$prefix}{$table_plura}') as table_exists");
131131
$exists = $con->select("SELECT to_regclass('{$schema}.{$prefix}{$table}') as table_exists");
132-
132+
133133
if (!empty($exists_plura[0]->table_exists)) {
134-
$table_val = "'$table'";
134+
$table_val = "'$table_plura'";
135135
$table = "{$prefix}{$table_plura}";
136136
} else if (!empty($exists[0]->table_exists)) {
137137
$table_val = "'$table'";
@@ -140,7 +140,7 @@ protected function createModel($class, $namespace, $file, $connection = null)
140140
} else {
141141
// MySQL 表检查
142142
if ($con->select("show tables like '{$prefix}{$table_plura}'")) {
143-
$table_val = "'$table'";
143+
$table_val = "'$table_plura'";
144144
$table = "{$prefix}{$table_plura}";
145145
} else if ($con->select("show tables like '{$prefix}{$table}'")) {
146146
$table_val = "'$table'";
@@ -157,7 +157,7 @@ protected function createModel($class, $namespace, $file, $connection = null)
157157
$comments = $tableComment[0]->table_comment;
158158
$properties .= " * {$table} {$comments}" . PHP_EOL;
159159
}
160-
160+
161161
// PostgreSQL 列信息
162162
$columns = $con->select("
163163
SELECT
@@ -172,7 +172,7 @@ protected function createModel($class, $namespace, $file, $connection = null)
172172
AND a.attnum > 0 AND NOT a.attisdropped
173173
ORDER BY a.attnum
174174
");
175-
175+
176176
foreach ($columns as $item) {
177177
if ($item->column_key === 'PRI') {
178178
$pk = $item->column_name;
@@ -187,15 +187,15 @@ protected function createModel($class, $namespace, $file, $connection = null)
187187
}
188188
$properties .= " * @property $type \${$item->column_name} " . ($item->column_comment ?? '') . "\n";
189189
}
190-
190+
191191
} else {
192192
// MySQL 表注释
193193
$tableComment = $con->select('SELECT table_comment FROM information_schema.`TABLES` WHERE table_schema = ? AND table_name = ?', [$database, $table]);
194194
if (!empty($tableComment)) {
195195
$comments = $tableComment[0]->table_comment ?? $tableComment[0]->TABLE_COMMENT;
196196
$properties .= " * {$table} {$comments}" . PHP_EOL;
197197
}
198-
198+
199199
// MySQL 列信息
200200
foreach ($con->select("select COLUMN_NAME,DATA_TYPE,COLUMN_KEY,COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS where table_name = '$table' and table_schema = '$database' ORDER BY ordinal_position") as $item) {
201201
if ($item->COLUMN_KEY === 'PRI') {
@@ -289,7 +289,7 @@ protected function createTpModel($class, $namespace, $file, $connection = null)
289289
$prefix = config("$config_name.connections.$connection.prefix") ?? '';
290290
$database = config("$config_name.connections.$connection.database");
291291
$driver = config("$config_name.connections.$connection.type") ?? 'mysql';
292-
292+
293293
if ($is_thinkorm_v2) {
294294
$con = \support\think\Db::connect($connection);
295295
} else {
@@ -302,7 +302,7 @@ protected function createTpModel($class, $namespace, $file, $connection = null)
302302
$schema = config("$config_name.connections.$connection.schema") ?? 'public';
303303
$exists = $con->query("SELECT to_regclass('{$schema}.{$prefix}{$table}') as table_exists");
304304
$exists_plural = $con->query("SELECT to_regclass('{$schema}.{$prefix}{$table}s') as table_exists");
305-
305+
306306
if (!empty($exists[0]['table_exists'])) {
307307
$table = "{$prefix}{$table}";
308308
$table_val = "'$table'";
@@ -330,7 +330,7 @@ protected function createTpModel($class, $namespace, $file, $connection = null)
330330
$comments = $tableComment[0]['table_comment'];
331331
$properties .= " * {$table} {$comments}" . PHP_EOL;
332332
}
333-
333+
334334
// PostgreSQL 列信息
335335
$columns = $con->query("
336336
SELECT
@@ -345,7 +345,7 @@ protected function createTpModel($class, $namespace, $file, $connection = null)
345345
AND a.attnum > 0 AND NOT a.attisdropped
346346
ORDER BY a.attnum
347347
");
348-
348+
349349
foreach ($columns as $item) {
350350
if ($item['column_key'] === 'PRI') {
351351
$pk = $item['column_name'];
@@ -361,7 +361,7 @@ protected function createTpModel($class, $namespace, $file, $connection = null)
361361
$comments = $tableComment[0]['table_comment'] ?? $tableComment[0]['TABLE_COMMENT'];
362362
$properties .= " * {$table} {$comments}" . PHP_EOL;
363363
}
364-
364+
365365
// MySQL 列信息
366366
foreach ($con->query("select COLUMN_NAME,DATA_TYPE,COLUMN_KEY,COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS where table_name = '$table' and table_schema = '$database' ORDER BY ordinal_position") as $item) {
367367
if ($item['COLUMN_KEY'] === 'PRI') {
@@ -426,15 +426,15 @@ protected function getType(string $type)
426426
if (strpos($type, 'int') !== false) {
427427
return 'integer';
428428
}
429-
429+
430430
if (strpos($type, 'character varying') !== false || strpos($type, 'varchar') !== false) {
431431
return 'string';
432432
}
433-
433+
434434
if (strpos($type, 'timestamp') !== false) {
435435
return 'string';
436436
}
437-
437+
438438
switch ($type) {
439439
case 'varchar':
440440
case 'string':

0 commit comments

Comments
 (0)