From fb52e8a421c315c2e804f212c28a977a78157fba Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 19 Sep 2016 15:28:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E6=95=B0=E6=8D=AE=E8=A1=A8?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=BC=93=E5=AD=98=E7=94=9F=E6=88=90=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AF=BB=E5=8F=96=E6=A8=A1=E5=9D=97=E7=9A=84?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think/console/command/optimize/Schema.php | 72 +++++++++++++++---- library/think/db/Query.php | 3 + 2 files changed, 61 insertions(+), 14 deletions(-) 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')) {