diff --git a/library/think/console/command/optimize/Schema.php b/library/think/console/command/optimize/Schema.php
index a81d6d57..2da00037 100644
--- a/library/think/console/command/optimize/Schema.php
+++ b/library/think/console/command/optimize/Schema.php
@@ -10,6 +10,7 @@
// +----------------------------------------------------------------------
namespace think\console\command\optimize;
+use think\App;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
@@ -26,35 +27,78 @@ class Schema extends Command
$this->setName('optimize:schema')
->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
+ ->addOption('module', null, Option::VALUE_REQUIRED, 'module name .')
->setDescription('Build database schema cache.');
}
protected function execute(Input $input, Output $output)
{
- if ($input->hasOption('table')) {
- $tables[] = $input->getOption('table');
- } else {
- if ($input->hasOption('db')) {
- $dbName = $input->getOption('db');
- $tables = Db::getTables($dbName);
- } else {
- $tables = Db::getTables();
- }
- }
-
if (!is_dir(RUNTIME_PATH . 'schema')) {
@mkdir(RUNTIME_PATH . 'schema', 0755, true);
}
+ if ($input->hasOption('module')) {
+ $module = $input->getOption('module');
+ // 读取模型
+ $list = scandir(APP_PATH . $module . DS . 'model');
+ $app = App::$namespace;
+ foreach ($list as $file) {
+ if ('.' == $file || '..' == $file) {
+ continue;
+ }
+ $class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
+ $this->buildModelSchema($class);
+ }
+ $output->writeln('Succeed!');
+ return;
+ } else if ($input->hasOption('table')) {
+ $table = $input->getOption('table');
+ if (!strpos($table, '.')) {
+ $dbName = Db::getConfig('database');
+ }
+ $tables[] = $table;
+ } elseif ($input->hasOption('db')) {
+ $dbName = $input->getOption('db');
+ $tables = Db::getTables($dbName);
+ } elseif (!\think\Config::get('app_multi_module')) {
+ $app = App::$namespace;
+ $list = scandir(APP_PATH . 'model');
+ foreach ($list as $file) {
+ if ('.' == $file || '..' == $file) {
+ continue;
+ }
+ $class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
+ $this->buildModelSchema($class);
+ }
+ $output->writeln('Succeed!');
+ return;
+ } else {
+ $dbName = Db::getConfig('database');
+ $tables = Db::getTables();
+ }
$db = isset($dbName) ? $dbName . '.' : '';
+ $this->buildDataBaseSchema($tables, $db);
+
+ $output->writeln('Succeed!');
+ }
+
+ protected function buildModelSchema($class)
+ {
+ $table = $class::getTable();
+ $dbName = $class::getConfig('database');
+ $content = 'getFields($table);
+ $content .= var_export($info, true) . ';';
+ file_put_contents(RUNTIME_PATH . 'schema' . DS . $dbName . '.' . $table . EXT, $content);
+ }
+
+ protected function buildDataBaseSchema($tables, $db)
+ {
foreach ($tables as $table) {
$content = 'writeln('Succeed!');
}
-
}
diff --git a/library/think/db/Query.php b/library/think/db/Query.php
index 04a8184c..7cb5bcf5 100644
--- a/library/think/db/Query.php
+++ b/library/think/db/Query.php
@@ -1341,6 +1341,9 @@ class Query
}
list($guid) = explode(' ', $tableName);
+ if (!strpos($guid, '.')) {
+ $guid = $this->getConfig('database') . '.' . $guid;
+ }
if (!isset(self::$info[$guid])) {
// 读取缓存
if (is_file(RUNTIME_PATH . 'schema/' . $guid . '.php')) {