This commit is contained in:
thinkphp
2017-03-23 12:01:32 +08:00
2 changed files with 13 additions and 8 deletions

View File

@@ -1300,7 +1300,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public static function get($data, $with = [], $cache = false) public static function get($data, $with = [], $cache = false)
{ {
if (is_null($data)) { if (is_null($data)) {
return null; return;
} }
if (true === $with || is_int($with)) { if (true === $with || is_int($with)) {

View File

@@ -25,6 +25,7 @@ class Schema extends Command
protected function configure() protected function configure()
{ {
$this->setName('optimize:schema') $this->setName('optimize:schema')
->addOption('config', null, Option::VALUE_REQUIRED, 'db config .')
->addOption('db', null, Option::VALUE_REQUIRED, 'db name .') ->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
->addOption('table', null, Option::VALUE_REQUIRED, 'table name .') ->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
->addOption('module', null, Option::VALUE_REQUIRED, 'module name .') ->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
@@ -36,6 +37,10 @@ class Schema extends Command
if (!is_dir(RUNTIME_PATH . 'schema')) { if (!is_dir(RUNTIME_PATH . 'schema')) {
@mkdir(RUNTIME_PATH . 'schema', 0755, true); @mkdir(RUNTIME_PATH . 'schema', 0755, true);
} }
$config = [];
if ($input->hasOption('config')) {
$config = $input->getOption('config');
}
if ($input->hasOption('module')) { if ($input->hasOption('module')) {
$module = $input->getOption('module'); $module = $input->getOption('module');
// 读取模型 // 读取模型
@@ -53,12 +58,12 @@ class Schema extends Command
} elseif ($input->hasOption('table')) { } elseif ($input->hasOption('table')) {
$table = $input->getOption('table'); $table = $input->getOption('table');
if (!strpos($table, '.')) { if (!strpos($table, '.')) {
$dbName = Db::getConfig('database'); $dbName = Db::connect($config)->getConfig('database');
} }
$tables[] = $table; $tables[] = $table;
} elseif ($input->hasOption('db')) { } elseif ($input->hasOption('db')) {
$dbName = $input->getOption('db'); $dbName = $input->getOption('db');
$tables = Db::getTables($dbName); $tables = Db::connect($config)->getTables($dbName);
} elseif (!\think\Config::get('app_multi_module')) { } elseif (!\think\Config::get('app_multi_module')) {
$app = App::$namespace; $app = App::$namespace;
$list = scandir(APP_PATH . 'model'); $list = scandir(APP_PATH . 'model');
@@ -72,11 +77,11 @@ class Schema extends Command
$output->writeln('<info>Succeed!</info>'); $output->writeln('<info>Succeed!</info>');
return; return;
} else { } else {
$tables = Db::getTables(); $tables = Db::connect($config)->getTables();
} }
$db = isset($dbName) ? $dbName . '.' : ''; $db = isset($dbName) ? $dbName . '.' : '';
$this->buildDataBaseSchema($tables, $db); $this->buildDataBaseSchema($tables, $db, $config);
$output->writeln('<info>Succeed!</info>'); $output->writeln('<info>Succeed!</info>');
} }
@@ -94,16 +99,16 @@ class Schema extends Command
} }
} }
protected function buildDataBaseSchema($tables, $db) protected function buildDataBaseSchema($tables, $db, $config)
{ {
if ('' == $db) { if ('' == $db) {
$dbName = Db::getConfig('database') . '.'; $dbName = Db::connect($config)->getConfig('database') . '.';
} else { } else {
$dbName = $db; $dbName = $db;
} }
foreach ($tables as $table) { foreach ($tables as $table) {
$content = '<?php ' . PHP_EOL . 'return '; $content = '<?php ' . PHP_EOL . 'return ';
$info = Db::getFields($db . $table); $info = Db::connect($config)->getFields($db . $table);
$content .= var_export($info, true) . ';'; $content .= var_export($info, true) . ';';
file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content); file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . $table . EXT, $content);
} }