Home > Backend Development > PHP Tutorial > PHP reads the table structure and automatically generates PHP classes

PHP reads the table structure and automatically generates PHP classes

WBOY
Release: 2016-07-28 08:28:56
Original
1158 people have browsed it

When I usually write projects, I often encounter operating databases. The most troublesome thing every time I operate a database is to write a table mapping class. Based on the rules, I wrote an automatically generated small demo:

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

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

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

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

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

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

    <span>$result</span> .= <span>"\n"</span>;

    <span>// get set</span><span>foreach</span> (<span>$res</span><span>as</span><span>$v</span>) {
        <span>$result</span> .= <span>"\n public function set"</span> . <span>$v</span>[<span>'Field'</span>] . <span>'($_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>"){"</span>;
        <span>$result</span> .= <span>"\n"</span> . <span>' $this->_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>'=$_'</span> . strtolower(<span>$v</span>[<span>'Field'</span>]) . <span>";"</span>;
        <span>$result</span> .= <span>"\n }"</span>;

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

        <span>$result</span> .= <span>"\n"</span>;
    }

    <span>$result</span> .= <span>" \n }"</span>;
    <span>$result</span> .= <span>" \n ?>"</span>;

    <span>return</span><span>$result</span>;
}

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

<span>/**
 * * 连接数据库,查询表结构
 *
 *<span> @param</span> unknown $array
 *            数据库连接参数
 *<span> @param</span> unknown $tableName
 *            表名
 */</span><span><span>function</span><span>linkdb</span><span>(<span>$array</span>, <span>$tableName</span>)</span>
{</span><span>$mysql_server_name</span> = <span>$array</span>[<span>'host'</span>]; <span>// 改成自己的mysql数据库服务器</span><span>$mysql_username</span> = <span>$array</span>[<span>'username'</span>]; <span>// 改成自己的mysql数据库用户名</span><span>$mysql_password</span> = <span>$array</span>[<span>'password'</span>]; <span>// 改成自己的mysql数据库密码</span><span>$mysql_database</span> = <span>$array</span>[<span>'dbname'</span>]; <span>// 改成自己的mysql数据库名</span><span>$conn</span> = mysql_connect(<span>$mysql_server_name</span>, <span>$mysql_username</span>, <span>$mysql_password</span>) <span>or</span><span>die</span>(<span>"error connecting"</span>); <span>// 连接数据库</span>
    mysql_query(<span>"set names "</span> . <span>$array</span>[<span>'charset'</span>]); <span>// 数据库输出编码</span>
    mysql_select_db(<span>$mysql_database</span>); <span>// 打开数据库</span><span>$sql</span> = <span>"SHOW FULL COLUMNS FROM $tableName"</span>;
    <span>$res</span> = mysql_query(<span>$sql</span>);
    <span>$res</span> = dataTable(<span>$res</span>);

    <span>return</span><span>$res</span>;
}

<span>/**
 * 返回到表集合
 *
 *<span> @param</span> query $query            
 *<span> @return</span> array
 */</span><span><span>function</span><span>dataTable</span><span>(<span>$query</span>)</span>
{</span><span>if</span> (<span>$query</span>) {
        <span>$ListTable</span> = <span>array</span>();
        <span>while</span> (<span>$rows</span> = mysql_fetch_array(<span>$query</span>, MYSQL_ASSOC)) {
            array_push(<span>$ListTable</span>, <span>$rows</span>);
        }
        <span>return</span><span>$ListTable</span>;
    } <span>else</span> {
        <span>return</span><span>0</span>;
    }
}

<span>/**
 * 调试函数
 *
 *<span> @param</span> unknown $msg            
 */</span><span><span>function</span><span>show_bug</span><span>(<span>$msg</span>)</span>
{</span><span>echo</span><span>'<pre class="brush:php;toolbar:false">'</span>;
    var_dump(<span>$msg</span>);
    <span>echo</span><span>'
'; }
Copy after login

The final effect:

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

    <span>public</span><span><span>function</span><span>getUserId</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_userid;
    }

    <span>public</span><span><span>function</span><span>setUserName</span><span>(<span>$_username</span>)</span>
    {</span><span>$this</span>->_username = <span>$_username</span>;
    }

    <span>public</span><span><span>function</span><span>getUserName</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_username;
    }

    <span>public</span><span><span>function</span><span>setPassWord</span><span>(<span>$_password</span>)</span>
    {</span><span>$this</span>->_password = <span>$_password</span>;
    }

    <span>public</span><span><span>function</span><span>getPassWord</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_password;
    }

    <span>public</span><span><span>function</span><span>setEmail</span><span>(<span>$_email</span>)</span>
    {</span><span>$this</span>->_email = <span>$_email</span>;
    }

    <span>public</span><span><span>function</span><span>getEmail</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_email;
    }

    <span>public</span><span><span>function</span><span>setMobile</span><span>(<span>$_mobile</span>)</span>
    {</span><span>$this</span>->_mobile = <span>$_mobile</span>;
    }

    <span>public</span><span><span>function</span><span>getMobile</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_mobile;
    }

    <span>public</span><span><span>function</span><span>setIP</span><span>(<span>$_ip</span>)</span>
    {</span><span>$this</span>->_ip = <span>$_ip</span>;
    }

    <span>public</span><span><span>function</span><span>getIP</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_ip;
    }

    <span>public</span><span><span>function</span><span>setStatus</span><span>(<span>$_status</span>)</span>
    {</span><span>$this</span>->_status = <span>$_status</span>;
    }

    <span>public</span><span><span>function</span><span>getStatus</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_status;
    }

    <span>public</span><span><span>function</span><span>setAreaId</span><span>(<span>$_areaid</span>)</span>
    {</span><span>$this</span>->_areaid = <span>$_areaid</span>;
    }

    <span>public</span><span><span>function</span><span>getAreaId</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_areaid;
    }

    <span>public</span><span><span>function</span><span>setAddTime</span><span>(<span>$_addtime</span>)</span>
    {</span><span>$this</span>->_addtime = <span>$_addtime</span>;
    }

    <span>public</span><span><span>function</span><span>getAddTime</span><span>()</span>
    {</span><span>return</span><span>$this</span>->_addtime;
    }
}
<span>?></span></span></code>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how PHP reads table structures and automatically generates PHP classes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template