From 448eaad5f52cbcc708345e46a758d698d19506e3 Mon Sep 17 00:00:00 2001 From: Karson Date: Mon, 25 Mar 2024 15:26:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96CRUD=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化代码 --- application/admin/command/Addon.php | 8 +++----- application/admin/command/Crud.php | 20 +++++++++++++------- application/admin/controller/Addon.php | 5 ++++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/application/admin/command/Addon.php b/application/admin/command/Addon.php index fb7c437c..7bca186e 100644 --- a/application/admin/command/Addon.php +++ b/application/admin/command/Addon.php @@ -15,7 +15,6 @@ use think\exception\PDOException; class Addon extends Command { - protected function configure() { $this @@ -33,6 +32,7 @@ class Addon extends Command protected function execute(Input $input, Output $output) { + \think\Config::load(dirname(dirname(__FILE__)) . DS . 'config.php'); $name = $input->getOption('name') ?: ''; $action = $input->getOption('action') ?: ''; if (stripos($name, 'addons' . DS) !== false) { @@ -82,7 +82,6 @@ class Addon extends Command $createTableSql = $result[0]['Create Table']; } } catch (PDOException $e) { - } $data = [ @@ -177,12 +176,12 @@ class Addon extends Command if (!$info) { throw new Exception(__('Addon info file data incorrect')); } - $infoname = isset($info['name']) ? $info['name'] : ''; + $infoname = $info['name'] ?? ''; if (!$infoname || !preg_match("/^[a-z]+$/i", $infoname) || $infoname != $name) { throw new Exception(__('Addon info name incorrect')); } - $infoversion = isset($info['version']) ? $info['version'] : ''; + $infoversion = $info['version'] ?? ''; if (!$infoversion || !preg_match("/^\d+\.\d+\.\d+$/i", $infoversion)) { throw new Exception(__('Addon info version incorrect')); } @@ -340,5 +339,4 @@ class Addon extends Command { return __DIR__ . '/Addon/stubs/' . $name . '.stub'; } - } diff --git a/application/admin/command/Crud.php b/application/admin/command/Crud.php index 150ea04a..9b4290bb 100755 --- a/application/admin/command/Crud.php +++ b/application/admin/command/Crud.php @@ -466,7 +466,7 @@ class Crud extends Command } } $relationTableInfo = $relationTableInfo[0]; - $relationModel = isset($relationModels[$index]) ? $relationModels[$index] : ''; + $relationModel = $relationModels[$index] ?? ''; list($relationNamespace, $relationName, $relationFile) = $this->getModelData($modelModuleName, $relationModel, $relationName); @@ -666,8 +666,8 @@ class Crud extends Command //如果是关联模型 foreach ($relations as $index => &$relation) { if ($relation['relationMode'] == 'hasone') { - $relationForeignKey = $relation['relationForeignKey'] ? $relation['relationForeignKey'] : $table . "_id"; - $relationPrimaryKey = $relation['relationPrimaryKey'] ? $relation['relationPrimaryKey'] : $priKey; + $relationForeignKey = $relation['relationForeignKey'] ?: $table . "_id"; + $relationPrimaryKey = $relation['relationPrimaryKey'] ?: $priKey; if (!in_array($relationForeignKey, $relation['relationFieldList'])) { throw new Exception('relation table [' . $relation['relationTableName'] . '] must be contain field [' . $relationForeignKey . ']'); @@ -676,8 +676,8 @@ class Crud extends Command throw new Exception('table [' . $modelTableName . '] must be contain field [' . $relationPrimaryKey . ']'); } } elseif ($relation['relationMode'] == 'belongsto') { - $relationForeignKey = $relation['relationForeignKey'] ? $relation['relationForeignKey'] : Loader::parseName($relation['relationName']) . "_id"; - $relationPrimaryKey = $relation['relationPrimaryKey'] ? $relation['relationPrimaryKey'] : $relation['relationPriKey']; + $relationForeignKey = $relation['relationForeignKey'] ?: Loader::parseName($relation['relationName']) . "_id"; + $relationPrimaryKey = $relation['relationPrimaryKey'] ?: $relation['relationPriKey']; if (!in_array($relationForeignKey, $fieldArr)) { throw new Exception('table [' . $modelTableName . '] must be contain field [' . $relationForeignKey . ']'); } @@ -685,8 +685,8 @@ class Crud extends Command throw new Exception('relation table [' . $relation['relationTableName'] . '] must be contain field [' . $relationPrimaryKey . ']'); } } elseif ($relation['relationMode'] == 'hasmany') { - $relationForeignKey = $relation['relationForeignKey'] ? $relation['relationForeignKey'] : $table . "_id"; - $relationPrimaryKey = $relation['relationPrimaryKey'] ? $relation['relationPrimaryKey'] : $priKey; + $relationForeignKey = $relation['relationForeignKey'] ?: $table . "_id"; + $relationPrimaryKey = $relation['relationPrimaryKey'] ?: $priKey; if (!in_array($relationForeignKey, $relation['relationFieldList'])) { throw new Exception('relation table [' . $relation['relationTableName'] . '] must be contain field [' . $relationForeignKey . ']'); } @@ -901,6 +901,12 @@ class Crud extends Command $cssClassArr[] = 'selectpage'; $selectpageTable = substr($field, 0, strripos($field, '_')); $selectpageField = ''; + foreach ($relations as $index => $relation) { + if ($relation['relationForeignKey'] === $field) { + $selectpageTable = substr($relation['relationTableName'], strlen($prefix)); + break; + } + } $selectpageController = str_replace('_', '/', $selectpageTable); $attrArr['data-source'] = $selectpageController . "/index"; //如果是类型表需要特殊处理下 diff --git a/application/admin/controller/Addon.php b/application/admin/controller/Addon.php index 355ddab6..d0918c43 100644 --- a/application/admin/controller/Addon.php +++ b/application/admin/controller/Addon.php @@ -441,8 +441,11 @@ class Addon extends Backend } catch (\Exception $e) { } - $rows = isset($json['rows']) ? $json['rows'] : []; + $rows = $json['rows'] ?? []; foreach ($rows as $index => $row) { + if (!isset($row['name'])) { + continue; + } $onlineaddons[$row['name']] = $row; } Cache::set("onlineaddons", $onlineaddons, 600);