修改字段生成

This commit is contained in:
liaofei 2023-05-12 11:47:35 +08:00
parent 130decee2f
commit 8529bb7543
2 changed files with 40 additions and 9 deletions

View File

@ -317,24 +317,41 @@ class SystemCrud extends AuthController
if ($newColumn) {
$info['field']['tableField'] = array_merge($newColumn, $info['field']['tableField']);
}
$keyInfo = [];
$deleteInfo = [];
foreach ($info['field']['tableField'] as $key => $item) {
$keyInfo = $deleteInfo = $createInfo = $updateInfo = [];
$tableField = [];
foreach ($info['field']['tableField'] as $item) {
if ($item['primaryKey']) {
$keyInfo = $item;
unset($info['field']['tableField'][$key]);
continue;
}
if ($item['field_type'] == 'addSoftDelete') {
if ($item['field_type'] == 'timestamp' && $item['field'] === 'delete_time') {
$deleteInfo = $item;
unset($info['field']['tableField'][$key]);
continue;
}
if ($item['field_type'] == 'timestamp' && $item['field'] === 'create_time') {
$createInfo = $item;
continue;
}
if ($item['field_type'] == 'timestamp' && $item['field'] === 'update_time') {
$updateInfo = $item;
continue;
}
$tableField[] = $item;
}
if ($keyInfo) {
array_unshift($info['field']['tableField'], $keyInfo);
array_unshift($tableField, $keyInfo);
}
if ($createInfo) {
array_push($tableField, $createInfo);
}
if ($updateInfo) {
array_push($tableField, $updateInfo);
}
if ($deleteInfo) {
array_push($info['field']['tableField'], $deleteInfo);
array_push($tableField, $deleteInfo);
}
$info['field']['tableField'] = $tableField;
$info['field']['pid'] = (int)$info['field']['pid'];
return app('json')->success(['file' => $data, 'crudInfo' => $info]);
}

View File

@ -352,7 +352,21 @@ class SystemCrudServices extends BaseServices
if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
$sql = "ALTER TABLE `$tableName` ADD `$field` $type NULL COMMENT '$comment' $after;";
} else {
$sql = "ALTER TABLE `$tableName` ADD `$field` $type($limit) NOT NULL DEFAULT '$default' COMMENT '$comment' $after;";
$defaultSql = "NOT NULL DEFAULT '$default'";
//处理时间字段默认值
if (in_array(strtolower($type), ['datetime', 'timestamp', 'time', 'date', 'year'])) {
switch ($field) {
case 'delete_time':
$defaultSql = 'NULL DEFAULT NULL';
break;
case 'create_time':
case 'update_time':
$defaultSql = 'NOT NULL DEFAULT CURRENT_TIMESTAMP';
break;
}
}
$limitSql = $limit ? '(' . $limit . ')' : '';
$sql = "ALTER TABLE `$tableName` ADD `$field` $type$limitSql $defaultSql COMMENT '$comment' $after;";
}
return Db::execute($sql);
}