This commit is contained in:
2023-10-07 20:33:15 +08:00
parent 8c1d85cad8
commit 38ae3d2031
+15 -21
View File
@@ -66,12 +66,10 @@ namespace {$namespace};
} }
} else { } else {
$import = "use Exception; $import = "use Exception;
use " . (str_replace('Controller', 'Form', $namespace)) . "\\{$managerName}Form;
use Kiri\Core\Str; use Kiri\Core\Str;
use Kiri\Core\Json; use Kiri\Core\Json;
use Kiri\Router\Base\Controller; use Kiri\Router\Base\Controller;
use {$model_namespace}\\{$managerName}; use {$model_namespace}\\{$managerName};
use Kiri\Router\Validator\BindForm;
use Kiri\Router\Validator\Validator; use Kiri\Router\Validator\Validator;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@@ -203,21 +201,17 @@ class {$controllerName}Controller extends Controller
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
$this->getData($path, $className, $fields); // $this->getData($path, $className, $fields);
return ' return '
/** /**
* @param Validator $form
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
public function actionAdd(#[BindForm(' . $className . 'Form::class)] Validator $form): ResponseInterface public function actionAdd(): ResponseInterface
{ {
if (!$form->run($this->request)) {
return $this->response->json([\'code\' => 500, \'message\' => $form->error()]);
}
$model = new ' . $className . '(); $model = new ' . $className . '();
$model->attributes = $form->getFormData()->toArray(); $model->attributes = $this->request->all();
if (!$model->save()) { if (!$model->save()) {
return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]); return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]);
} else { } else {
@@ -288,20 +282,16 @@ class {$controllerName}Controller extends Controller
return ' return '
/** /**
* @param Validator $form
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
public function actionUpdate(#[BindForm(' . $className . 'Form::class)] Validator $form): ResponseInterface public function actionUpdate(): ResponseInterface
{ {
if (!$form->run($this->request)) {
return $this->response->json([\'code\' => 500, \'message\' => $form->error()]);
}
$model = ' . $className . '::findOne($this->request->post(\'id\', 0)); $model = ' . $className . '::findOne($this->request->post(\'id\', 0));
if (empty($model)) { if (empty($model)) {
return $this->response->json([\'code\' => 500, \'message\' => SELECT_IS_NULL]); return $this->response->json([\'code\' => 500, \'message\' => SELECT_IS_NULL]);
} }
$model->attributes = $form->getFormData()->toArray(); $model->attributes = $this->request->all();
if (!$model->save()) { if (!$model->save()) {
return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]); return $this->response->json([\'code\' => 500, \'message\' => $model->getLastError()]);
} else { } else {
@@ -526,7 +516,7 @@ class {$controllerName}Controller extends Controller
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
#[Length(10)] #[Length(10)]
public ?string $' . $val['Field'] . ' = null; public string $' . $val['Field'] . ' = \'1960-06-01\';
', ',
'time' => ' 'time' => '
@@ -534,7 +524,7 @@ class {$controllerName}Controller extends Controller
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
#[Length(5)] #[Length(5)]
public ?string $' . $val['Field'] . ' = null; public string $' . $val['Field'] . ' = \'00:00\';
', ',
default => ' default => '
@@ -542,7 +532,7 @@ class {$controllerName}Controller extends Controller
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
#[Length(16)] #[Length(16)]
public ?string $' . $val['Field'] . ' = null; public string $' . $val['Field'] . ' = \'\';
', ',
}; };
@@ -551,7 +541,7 @@ class {$controllerName}Controller extends Controller
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
public ?array $' . $val['Field'] . ' = null; public string $' . $val['Field'] . ' = \'\';
'; ';
} else { } else {
@@ -577,10 +567,14 @@ class {$controllerName}Controller extends Controller
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/' . ($type == 'enum' ? ' */' . ($type == 'enum' ? '
#[In([' . $number[0] . '])]' : '') . '' . ($_field['required'] == 'true' ? ' #[In([' . $number[0] . '])]' : '') . ($_field['required'] == 'true' ? '
#[Required]' : '') . ' #[Required]' : '') . '
#[MaxLength(' . ($number[1] ?? 0) . ')] #[MaxLength(' . ($number[1] ?? 0) . ')]
public ?' . $_key . ' $' . $val['Field'] . ' = null; public ' . $_key . ' $' . $val['Field'] . ' = '.(match ($type) {
'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp', 'year' => 0,
'bool', 'boolean' => false,
default => '\'\'',
}).';
'; ';
} }