生成数据库模型文件

//生成数据库模型;
 

$con = mysqli_connect("{host}","{user}","{pass}","(database)",“{port}”);
if(!$con){
die('database connect error!');
}
$sqlTableModel = [];
$result = mysqli_query($con,"SHOW TABLES");

while($row = mysqli_fetch_array($result))
{
$sqlTableModel[]= table_str_func($row[0]);
}
//dump($sqlTableModel);die;
return [
// 生成应用公共文件
'__file__' => ['common.php', 'config.php', 'database.php'],
//生成数据库模型
'common' => [
'__dir__' => ['behavior','model','logic','service'],
'model' => array_merge(["Base"],$sqlTableModel),
'logic' => array_merge(["Base"],$sqlTableModel),
'service' => array_merge(["Base"],$sqlTableModel),
],
]
 
/**
* 接受数据库表名称,去除前缀并转换为首字母大写,必须规范"x_y_z"
* @param $str
* @return string
*/
function table_str_func($str)
{
$reg = '/^[a-zA-Z0-9]+_/';
$t = preg_replace ($reg, "" , $str);
$new_t = explode("_",$t);$new_str='';
foreach ($new_t as $keies){
$new_str .= ucfirst($keies);
}
return $new_str;
}
 
执行:php think build 
 
成功生成!
image
posted @ 2017-05-27 15:35  徐锅  阅读(330)  评论(0编辑  收藏  举报