mirror of https://gitee.com/karson/fastadmin.git
commit
840abb714d
|
|
@ -173,12 +173,15 @@ class Crud extends Command
|
||||||
->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null)
|
->addOption('sortfield', null, Option::VALUE_OPTIONAL, 'sort field', null)
|
||||||
->addOption('headingfilterfield', null, Option::VALUE_OPTIONAL, 'heading filter field', null)
|
->addOption('headingfilterfield', null, Option::VALUE_OPTIONAL, 'heading filter field', null)
|
||||||
->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null)
|
->addOption('editorclass', null, Option::VALUE_OPTIONAL, 'automatically generate editor class', null)
|
||||||
|
->addOption('db', null, Option::VALUE_OPTIONAL, 'database config name', 'database')
|
||||||
->setDescription('Build CRUD controller and model from table');
|
->setDescription('Build CRUD controller and model from table');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(Input $input, Output $output)
|
protected function execute(Input $input, Output $output)
|
||||||
{
|
{
|
||||||
$adminPath = dirname(__DIR__) . DS;
|
$adminPath = dirname(__DIR__) . DS;
|
||||||
|
//数据库
|
||||||
|
$db = $input->getOption('db');
|
||||||
//表名
|
//表名
|
||||||
$table = $input->getOption('table') ?: '';
|
$table = $input->getOption('table') ?: '';
|
||||||
//自定义控制器
|
//自定义控制器
|
||||||
|
|
@ -279,8 +282,9 @@ class Crud extends Command
|
||||||
|
|
||||||
$this->reservedField = array_merge($this->reservedField, [$this->createTimeField, $this->updateTimeField, $this->deleteTimeField]);
|
$this->reservedField = array_merge($this->reservedField, [$this->createTimeField, $this->updateTimeField, $this->deleteTimeField]);
|
||||||
|
|
||||||
$dbname = Config::get('database.database');
|
$dbconnect = Db::connect($db);
|
||||||
$prefix = Config::get('database.prefix');
|
$dbname = Config::get($db . '.database');
|
||||||
|
$prefix = Config::get($db . '.prefix');
|
||||||
|
|
||||||
//模块
|
//模块
|
||||||
$moduleName = 'admin';
|
$moduleName = 'admin';
|
||||||
|
|
@ -291,11 +295,11 @@ class Crud extends Command
|
||||||
$modelName = $table = stripos($table, $prefix) === 0 ? substr($table, strlen($prefix)) : $table;
|
$modelName = $table = stripos($table, $prefix) === 0 ? substr($table, strlen($prefix)) : $table;
|
||||||
$modelTableType = 'table';
|
$modelTableType = 'table';
|
||||||
$modelTableTypeName = $modelTableName = $modelName;
|
$modelTableTypeName = $modelTableName = $modelName;
|
||||||
$modelTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
|
$modelTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
|
||||||
if (!$modelTableInfo) {
|
if (!$modelTableInfo) {
|
||||||
$modelTableType = 'name';
|
$modelTableType = 'name';
|
||||||
$modelTableName = $prefix . $modelName;
|
$modelTableName = $prefix . $modelName;
|
||||||
$modelTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
|
$modelTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$modelTableName}'", [], true);
|
||||||
if (!$modelTableInfo) {
|
if (!$modelTableInfo) {
|
||||||
throw new Exception("table not found");
|
throw new Exception("table not found");
|
||||||
}
|
}
|
||||||
|
|
@ -312,11 +316,11 @@ class Crud extends Command
|
||||||
$relationName = stripos($relationTable, $prefix) === 0 ? substr($relationTable, strlen($prefix)) : $relationTable;
|
$relationName = stripos($relationTable, $prefix) === 0 ? substr($relationTable, strlen($prefix)) : $relationTable;
|
||||||
$relationTableType = 'table';
|
$relationTableType = 'table';
|
||||||
$relationTableTypeName = $relationTableName = $relationName;
|
$relationTableTypeName = $relationTableName = $relationName;
|
||||||
$relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
|
$relationTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
|
||||||
if (!$relationTableInfo) {
|
if (!$relationTableInfo) {
|
||||||
$relationTableType = 'name';
|
$relationTableType = 'name';
|
||||||
$relationTableName = $prefix . $relationName;
|
$relationTableName = $prefix . $relationName;
|
||||||
$relationTableInfo = Db::query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
|
$relationTableInfo = $dbconnect->query("SHOW TABLE STATUS LIKE '{$relationTableName}'", [], true);
|
||||||
if (!$relationTableInfo) {
|
if (!$relationTableInfo) {
|
||||||
throw new Exception("relation table not found");
|
throw new Exception("relation table not found");
|
||||||
}
|
}
|
||||||
|
|
@ -451,7 +455,7 @@ class Crud extends Command
|
||||||
. "WHERE TABLE_SCHEMA = ? AND table_name = ? "
|
. "WHERE TABLE_SCHEMA = ? AND table_name = ? "
|
||||||
. "ORDER BY ORDINAL_POSITION";
|
. "ORDER BY ORDINAL_POSITION";
|
||||||
//加载主表的列
|
//加载主表的列
|
||||||
$columnList = Db::query($sql, [$dbname, $modelTableName]);
|
$columnList = $dbconnect->query($sql, [$dbname, $modelTableName]);
|
||||||
$fieldArr = [];
|
$fieldArr = [];
|
||||||
foreach ($columnList as $k => $v) {
|
foreach ($columnList as $k => $v) {
|
||||||
$fieldArr[] = $v['COLUMN_NAME'];
|
$fieldArr[] = $v['COLUMN_NAME'];
|
||||||
|
|
@ -459,7 +463,7 @@ class Crud extends Command
|
||||||
|
|
||||||
// 加载关联表的列
|
// 加载关联表的列
|
||||||
foreach ($relations as $index => &$relation) {
|
foreach ($relations as $index => &$relation) {
|
||||||
$relationColumnList = Db::query($sql, [$dbname, $relation['relationTableName']]);
|
$relationColumnList = $dbconnect->query($sql, [$dbname, $relation['relationTableName']]);
|
||||||
|
|
||||||
$relationFieldList = [];
|
$relationFieldList = [];
|
||||||
foreach ($relationColumnList as $k => $v) {
|
foreach ($relationColumnList as $k => $v) {
|
||||||
|
|
@ -819,6 +823,7 @@ class Crud extends Command
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
|
'databaseConfigName' => $db,
|
||||||
'controllerNamespace' => $controllerNamespace,
|
'controllerNamespace' => $controllerNamespace,
|
||||||
'modelNamespace' => $modelNamespace,
|
'modelNamespace' => $modelNamespace,
|
||||||
'validateNamespace' => $validateNamespace,
|
'validateNamespace' => $validateNamespace,
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ class {%modelName%} extends Model
|
||||||
|
|
||||||
{%softDelete%}
|
{%softDelete%}
|
||||||
|
|
||||||
|
//数据库
|
||||||
|
protected $connection = '{%databaseConfigName%}';
|
||||||
// 表名
|
// 表名
|
||||||
protected ${%modelTableType%} = '{%modelTableTypeName%}';
|
protected ${%modelTableType%} = '{%modelTableTypeName%}';
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue