论坛首页 编程语言技术论坛

根据表结构自动生成一个PHP类

浏览 3985 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-16   最后修改:2011-12-26
PHP

 

<?php
/**
 * 这个类的作用是从数据库里读出表结构,然后生成一个bean类,并将其属性与类一一映射
 */
class TableClassGenerator {
    private $dbConn;
    private $file;
    
    public function __construct() {
        $this->dbConn = mysql_connect('localhost', 'root');
    }
    
    public function __destroy() {
        mysql_close($this->dbConn);
    }
    
    public static function newInstance() {
        return new TableClassGenerator();
    }

    public function loadDatabases () {
        $dbs = mysql_list_dbs($this->dbConn);
        while($db = mysql_fetch_object($dbs)) {
            echo $db->Database . '<br />';
        }
    }
    public function loadTableColumns($table) {
        $fields = mysql_list_fields('ucdream', $table, $this->dbConn);
        $count = mysql_num_fields($fields);
        if (! isset($fields)) {
            die("Failed to get fields" . mysql_error());
        }

        $columns = array();
        for ($i = 0; $i < $count; $i++) {
            $flags = mysql_field_flags($fields, $i);
            $isRequired = preg_match('/not_null/', $flags);

            $col = mysql_field_name($fields, $i);
            $columns[$col] = array(
                'isRequired' => $isRequired,
                'max' => mysql_field_len($fields, $i),
                'type' => mysql_field_type($fields, $i),
            );
        }
        
        $sortedColumns = array();
        $keys = array_keys($columns);
        sort($keys);
        foreach ($keys as $key) {
            $sortedColumns[$key] = $columns[$key];
        }
        return $sortedColumns;
    }
    
    private function transform($name) {
        $words = explode('_', $name);
        $newName = null;
        foreach ($words as $word) {
            if ($newName == null) {
                $newName = $word;
            } else {
                $newName .= ucfirst($word);
            }
        }
        
        return $newName;
    }
    
    private function generateProperties($columns) {
        $keys = array_keys($columns);
        foreach ($keys as $key) {
            $prop = $this->transform($key);
            $this->writeToFile("private $prop;", 1);
        }
        
        $this->writeToFile("\n");
    }
    
    private function generateGetters($columns) {
        foreach ($columns as $key => $value) {
            $prop = $this->transform($key);
            $method = 'get' . ucfirst($prop);
            $this->writeToFile("public function $method() {", 1);
            $this->writeToFile('return $' . "this->$prop;", 2);
            $this->writeToFile("}\n", 1);
        }
    }
    
    private function generateSetters($columns) {
        foreach ($columns as $key => $value) {
            $prop = $this->transform($key);
            $method = 'set' . ucfirst($prop);
            $this->writeToFile("public function $method ($$prop) {", 1);
            $this->writeToFile('$' . "this->$prop = $" . "$prop;", 2);
            $this->writeToFile("}\n", 1);
        }
    }
    
    private function generateColumnPropMapping($columns) {
        
    }
    
    private function writeToFile($str, $count = 0) {
        $space = null;
        $count *= 4;
        while ($count) {
            if ($space == null) {
                $space = ' ';
            } else {
                $space .= ' ';
            }
            $count--;
        }
        fwrite($this->file, $space);
        fwrite($this->file, "$str\n");
    }
    
    public function generateCode() {
        $table = 'user';
        $columns = $this->loadTableColumns('ucd_user');
        //print_r($columns);
        
        $class = ucfirst($this->transform($table));
        $this->file = fopen("$class.php", 'w');
        $this->writeToFile("<?php");
        $this->writeToFile("class $class extends BaseObject {");
        $this->generateProperties($columns);
        $this->generateGetters($columns);
        $this->generateSetters($columns);
        $this->writeToFile("}");
        $this->writeToFile("?>");
        fclose($this->file);
    }
}
   发表时间:2011-12-25  
赞。这个创意很好。
对ORM很用作用。
0 请登录后投票
   发表时间:2012-01-05  
ORM 就是搞这个东西的
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics