当前位置:Gxlcms > PHP教程 > php读取表结构自动生成php类

php读取表结构自动生成php类

时间:2021-07-01 10:21:17 帮助过:8人阅读

平时写项目的时候经常会碰到操作数据库,每次操作数据库的时候最麻烦的就是要写一个表映射的类,根据规律总结写了一个自动生成的小demo:

// 数据库配置参数$db_config = array(
    'host' => '127.0.0.1',
    'port' => 3306,
    'username' => 'root',
    'password' => '',
    'dbname' => 'joe_db',
    'charset' => 'utf8'
);

$tablePre = 'joe_'; // 表前缀$tableName = 'users'; // 表名$res = linkdb($db_config, $tablePre . $tableName);

$className = $tableName . 'Dmodel';
$fileName = $tableName . '.dm.class.php';
$annStr = addAnnotation($fileName, $tableName, '用户session表');
$classStr = readTableStr($res, $className,$annStr);

// 写入文件$file = fopen($fileName, "w+");
fwrite($file, $classStr);
fclose($file);

/**
 * 根据表结构初始化表对象
 *
 * @param unknown $res            
 * @param unknown $className            
 * @return string
 */functionreadTableStr($res, $className, $annStr='')
{$result = ' . "\n";
    $result .= $annStr;
    $result .= "class $className \n{";

    // 变量foreach ($resas$v) {
        $result .= "\n" . 'private $_' . strtolower($v['Field']) . ";//" . $v['Comment'] . $v['Type'];
    }

    $result .= "\n";

    // get setforeach ($resas$v) {
        $result .= "\n public function set" . $v['Field'] . '($_' . strtolower($v['Field']) . "){";
        $result .= "\n" . ' $this->_' . strtolower($v['Field']) . '=$_' . strtolower($v['Field']) . ";";
        $result .= "\n }";

        $result .= "\n public function get" . $v['Field'] . '(){';
        $result .= "\n" . ' return $this->_' . strtolower($v['Field']) . ";";
        $result .= "\n }";

        $result .= "\n";
    }

    $result .= " \n }";
    $result .= " \n ?>";

    return$result;
}

/**
 * 添加类注释
 * 
 * @param unknown $fileName            
 * @param unknown $fun            
 * @param unknown $des            
 * @param unknown $date            
 * @param unknown $author            
 */functionaddAnnotation($fileName, $fun, $des)
{$annStr =  "\n/**";
    $annStr .= "\n* 文件名:" . $fileName;
    $annStr .= "\n* 功能:    模型层-表-" . $fun;
    $annStr .= "\n* 描述:    " . $des;
    $annStr .= "\n* 日期:    " . date('y-m-d', time());
    $annStr .= "\n* 版权:    Copyright ? 2016 github.com/JoeXiong All Rights Reserved";
    $annStr .= "\n* @author JoeXiong";
    $annStr .= "\n*/";
    return$annStr;
}

/**
 * * 连接数据库,查询表结构
 *
 * @param unknown $array
 *            数据库连接参数
 * @param unknown $tableName
 *            表名
 */functionlinkdb($array, $tableName)
{$mysql_server_name = $array['host']; // 改成自己的mysql数据库服务器$mysql_username = $array['username']; // 改成自己的mysql数据库用户名$mysql_password = $array['password']; // 改成自己的mysql数据库密码$mysql_database = $array['dbname']; // 改成自己的mysql数据库名$conn = mysql_connect($mysql_server_name, $mysql_username, $mysql_password) ordie("error connecting"); // 连接数据库
    mysql_query("set names " . $array['charset']); // 数据库
输出编码 mysql_select_db($mysql_database); // 打开数据库$sql = "SHOW FULL COLUMNS FROM $tableName"; $res = mysql_query($sql); $res = dataTable($res); return$res; } /** * 返回到表集合 * * @param query $query * @return array */functiondataTable($query) {if ($query) { $ListTable = array(); while ($rows = mysql_fetch_array($query, MYSQL_ASSOC)) { array_push($ListTable, $rows); } return$ListTable; } else { return0; } } /** * 调试函数 * * @param unknown $msg */functionshow_bug($msg) {echo'
';
    var_dump($msg);
    echo'
'
; }

最终实现效果:

/**
* 文件名:users.dm.class.php
* 功能:    模型层-表-users
* 描述:    用户session表
* 日期:    16-06-27
* 版权:    Copyright ? 2016 github.com/JoeXiong All Rights Reserved
* @author JoeXiong
*/classusersDmodel
{private$_userid; // int(11) unsignedprivate$_username; // 用户名varchar(50)private$_password; // 密码varchar(32)private$_email; // 邮箱varchar(80)private$_mobile; // 手机号码varchar(20)private$_ip; // 用户注册IDvarchar(30)private$_status; // 帐号状态tinyint(1) unsignedprivate$_areaid; // 区域Id,10001为本区;10002为qq用户;10003为新浪微博用户smallint(5) unsignedprivate$_addtime; // 创建时间int(10) unsignedpublicfunctionsetUserId($_userid)
    {$this->_userid = $_userid;
    }

    publicfunctiongetUserId()
    {return$this->_userid;
    }

    publicfunctionsetUserName($_username)
    {$this->_username = $_username;
    }

    publicfunctiongetUserName()
    {return$this->_username;
    }

    publicfunctionsetPassWord($_password)
    {$this->_password = $_password;
    }

    publicfunctiongetPassWord()
    {return$this->_password;
    }

    publicfunctionsetEmail($_email)
    {$this->_email = $_email;
    }

    publicfunctiongetEmail()
    {return$this->_email;
    }

    publicfunctionsetMobile($_mobile)
    {$this->_mobile = $_mobile;
    }

    publicfunctiongetMobile()
    {return$this->_mobile;
    }

    publicfunctionsetIP($_ip)
    {$this->_ip = $_ip;
    }

    publicfunctiongetIP()
    {return$this->_ip;
    }

    publicfunctionsetStatus($_status)
    {$this->_status = $_status;
    }

    publicfunctiongetStatus()
    {return$this->_status;
    }

    publicfunctionsetAreaId($_areaid)
    {$this->_areaid = $_areaid;
    }

    publicfunctiongetAreaId()
    {return$this->_areaid;
    }

    publicfunctionsetAddTime($_addtime)
    {$this->_addtime = $_addtime;
    }

    publicfunctiongetAddTime()
    {return$this->_addtime;
    }
}
?>

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介绍了 php 读取表结构自动生成php类,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

  • 人气教程排行