根据表结构自动生成一个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 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 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();