ThinkPHP 5 代码自动生成字段缓存

use think\Db;
use think\Env;
// 更新字段缓存文件(Runtime目录在jenkins部署的时候会自动删除) private function _setColumnCache() { if (Env::get('enviroment')=='develop') return ; // 研发环境,不开启字段缓存 $config = config('database'); $dbName = $config['database']; $schemeDir = RUNTIME_PATH.'schema'; // 拿一个必定存在的表进行判断,如果有对应的缓存文件,则表示字段缓存已生成 $menuCacheFile = "$schemeDir/$dbName.$config[prefix]menu.php"; if (file_exists($menuCacheFile)) return true; // 生成字段缓存 if (!is_dir($schemeDir)) @mkdir($schemeDir, 0755, true); $tables = Db::connect($config)->getTables($dbName); foreach ($tables as $table) { $content = '<?php ' . PHP_EOL . 'return '; $info = Db::connect($config)->getFields($table); $content .= var_export($info, true) . ';'; file_put_contents($schemeDir . DS . $dbName . '.' . $table . EXT, $content); } } // 在需要的地方 $this->_setColumnCache(); 调用即可

 

posted @ 2022-03-29 13:12  Funsion Wu  Views(368)  Comments(0Edit  收藏  举报