依據表結構自動產生一個PHP類

來源:互聯網
上載者:User
根據表結構自動產生一個PHP類

Zend framework提供了一種class和table映射起來的方式,建立一個繼承Zend_Db_Table的class。查詢時,zend自動將表欄位做處理,產生一個對象,對象屬性都是動態建立,所以是public的。這有兩個大問題,一是class的屬性是public,二是class的屬性只有代碼執行後才確定。於是乎,自己寫了一個程式,根據表資訊產生對應的class。

?

?

database = $config['database'];    $conn = isset($config['password'])        ? mysql_connect($config['host'], $config['user'], $config['password'])    : mysql_connect($config['host'], $config['user']);    if (! isset($conn)) {    die('Failed to connect.' . mysql_error());    }        $this->givenTables = $config['tables'];    if (isset($this->givenTables)    && (!is_array($this->givenTables)    || empty($this->givenTables))) {    die("Tables($this->givenTables) in config is not an array or it is empty.");    }        $this->parentClass = $config['parentClass'];        if ($config['excludedProperties']) {    $this->excludedProperties = $config['excludedProperties'];    if (!is_array($this->excludedProperties)    || empty($this->excludedProperties)) {    die('excludedProperties should be an array and shoudnot be empty.');    }    }        if (! file_exists(self::DEFAULT_DIR)) {    mkdir(self::DEFAULT_DIR);    }    }        public function __destroy() {        mysql_close();    }    public function generateClasses() {$allTables = $this->getTables();var_dump($allTables);$tables = $this->givenTables? $this->givenTables: $allTables;    if (empty($tables)) {    die("Empty given tables");    }        foreach ($tables as $table) {    $index = array_search($table, $allTables);    if (!is_int($index)) {    echo "Table($table) not found in database({$this->database}).\n";    continue;    }        $this->generateClassForTable($table);    }    }    private function generateClassForTable($table) {        $class = ucfirst($this->transform($table));        $fileName = self::DEFAULT_DIR . "/$class.php";        if (file_exists($fileName)) {        echo "The file($fileName) already exists. You need delete if first.\n";        //return;        }                $columns = $this->getTableColumns($table);        if (!isset($columns) || empty($columns)) {  echo "The table($table) doesn't have columns.\n";        return;          }                $this->file = fopen($fileName, 'w');        if (! isset($this->file)) {        die("Failed to open file: $fileName");        }                echo "Generating class for table: $table.\n";        $this->writeToFile("parentClass) {        $this->writeToFile("class $class extends {$this->parentClass} {");        } else {        $this->writeToFile("class $class {");        }        $this->generateConst($table);        $this->generateColumnPropMapping($columns);        $this->generateValidateConfig($table, $columns);        $this->generateProperties($columns);        $this->generateGetters($columns);        $this->generateSetters($columns);        $this->writeToFile("}");        $this->writeToFile("?>");        fclose($this->file);        echo "Class($class) was created in the file($fileName).\n\n";    }    private function generateColumnPropMapping($columns) {$this->writeToFile('private static $_colPropMapping = array(', 1);foreach ($columns as $key => $value) {$prop = $this->transform($key);$this->writeToFile("'$key' => '$prop',", 2);}$this->writeToFile(');', 1);$this->writeNewLine();    }        private function generateConst($table) {    $this->writeToFile("const TABLE_NAME = '$table';", 1);    $this->writeNewLine();    }    private function generateGetters($columns) {        foreach ($columns as $key => $value) {            $prop = $this->transform($key);        if ($this->shouldExcludeProp($prop)) {            continue;            }                        $method = 'get' . ucfirst($prop);            $this->writeToFile("public function $method() {", 1);            $this->writeToFile('return $' . "this->$prop;", 2);            $this->writeToFile("}", 1);            $this->writeNewLine();        }    }        private function generateProperties($columns) {        $keys = array_keys($columns);        foreach ($keys as $key) {            $prop = $this->transform($key);            if ($this->shouldExcludeProp($prop)) {            continue;            }            $this->writeToFile("private $prop;", 1);        }        $this->writeNewLine();    }        private function generateSetters($columns) {        foreach ($columns as $key => $value) {            $prop = $this->transform($key);        if ($this->shouldExcludeProp($prop)) {            continue;            }            $method = 'set' . ucfirst($prop);            $this->writeToFile("public function $method ($$prop) {", 1);            $this->writeToFile('$' . "this->$prop = $" . "$prop;", 2);            $this->writeToFile("}", 1);            $this->writeNewLine();        }    }        private function generateValidateConfig($table, $columns) {    $this->writeToFile('private static $_validator = array(', 1);$this->writeToFile("'$table' => array(", 2);foreach ($columns as $key => $value) {$this->writeToFile("'$key' => array(", 3);foreach ($value as $k => $v) {if (is_string($v)) {$this->writeToFile("'$k' => '$v',", 4);} else {$this->writeToFile("'$k' => $v,", 4);}}$this->writeToFile("),", 3);}$this->writeToFile("), // $table", 2);$this->writeToFile(');', 1);$this->writeNewLine();    }        private function getMin($max, $type) {    if (!isset($max)) {    return null;    }        $min = self::DEFAULT_MIN;    if ($type == 'date' || $min > $max) {    $min = $max;    }    return $min;    }        public function getTableColumns($table) {        $fields = mysql_list_fields($this->database, $table);        $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);            $max = mysql_field_len($fields, $i);            $type = mysql_field_type($fields, $i);            $min = $this->getMin($max, $type);                        $columns[$col] = array(                'isRequired' => $isRequired,                'max' => $max,            'min' => $min,                'type' => $type,            );        }                $sortedColumns = array();        $keys = array_keys($columns);        sort($keys);        foreach ($keys as $key) {            $sortedColumns[$key] = $columns[$key];        }        return $sortedColumns;    }        private function getTables() {    $sql = "SHOW TABLES FROM {$this->database}";    $result = mysql_query($sql);    $tables = array();    for ($i = 0; $i < mysql_num_rows($result); $i++) {    $tables[] = mysql_tablename($result, $i);    }    return $tables;    }        private function shouldExcludeProp($prop) {    if (! isset($this->excludedProperties)) {    return false;    }            $index = array_search($prop, $this->excludedProperties);        return is_int($index);    }    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 writeNewLine() {    $this->writeToFile('');    }        private function writeToFile($str, $count = 0) {        $space = null;        $count *= self::DEFAULT_INDENT;        while ($count) {            if ($space == null) {                $space = ' ';            } else {                $space .= ' ';            }            $count--;        }        fwrite($this->file, $space);        fwrite($this->file, "$str\n");    }} // TableClassGenerator$gen = new TableClassGenerator(array(    'excludedProperties' => array('id', 'userId'),    'database' => 'mydb',    'host' => 'localhost',    'parentClass' => 'Base',    'password' => 'pwd',    // 'tables' => array('user'),    'user' => 'userId',));    $gen->generateClasses();
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.