a89910911

批量生成dbtable Zend framework 笔记



Zend framework等主流框架中一个数据表对应一个dbtable ,代码格式相似,可以利用php fopen函数自动生成

核心代码
$sql="SELECT TABLE_NAME,TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$db."'";
INFORMATION_SCHEMA.TABLES这张数据表保存了MySQL服务器所有数据库的信息,是MySQL的提供的数据库管理信息详情请转到
$db 就是接下来需要操作的数据库的名字 执行mysqli_fetch_all后返回一个一维数组

$txt='<?php  namespace ..............'
        $dbtablename = $dbtablename[$row['TABLE_NAME']].".php";
        $myfile = fopen($dbtablename , "w");
        fwrite($myfile, $txt);


以下为事例代码
<?php

$con = mysqli_connect();
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db = $_GET['db'];
$sql="SELECT TABLE_NAME,TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$db."'";

$result=mysqli_query($con,$sql);
$data = mysqli_fetch_all($result,MYSQLI_ASSOC);

function stre($string)
{
    $res = $string;
    foreach(range('a','z') as $v){
    $bv=strtoupper($v);
    $bere = "_".$v;
    $re ="_".$bv;
    $res = str_replace($bere,$re,$res);
    }
    return $res;
}
$dbtablename = array();
foreach ($data as $row ) {
    $dbtablename[$row['TABLE_NAME']] = str_replace("_","",str_replace("dl_","",stre($row['TABLE_NAME'])));
    $dbtablenamestring[$row['TABLE_NAME']] =    str_replace("dl_","",$row['TABLE_NAME']);   
        $txt='
<?php

namespace Model\Db;

use GatewayWorker\Lib\DbTable;
    /*
    * '.$row ["TABLE_COMMENT"].'
    */
class AppFeedback extends DbTable{

    protected $_name = "'.$dbtablenamestring[$row['TABLE_NAME']].'";

    protected $_primary = "id";

    protected $_db = "'.$db.'";

    /*
    * 构造函数
    */
    public function __construct(){
      
    }


    }   
        ';   
        $lastname = $dbtablename[$row['TABLE_NAME']].".php";
        $myfile = fopen($lastname, "w");
        fwrite($myfile, $txt);
}
  mysqli_close($con);
?>

评论