Z-Blog Wiki Z-Blog Wiki

Z-Blog官方文库

用户工具

站点工具


zblogphp:development:plugin:自定义数据库表

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

后一修订版
前一修订版
zblogphp:development:plugin:自定义数据库表 [2016/06/20 20:01]
127.0.0.1 外部编辑
zblogphp:development:plugin:自定义数据库表 [2020/06/30 10:57] (当前版本)
zx.asd
行 3: 行 3:
 <code php> <code php>
 #表名 #表名
-$table['​plugin_duoshuo_comment'​] = '​plugin_duoshuo_comment';​+$table['​plugin_duoshuo_comment'​] = '%pre%plugin_duoshuo_comment';​ 
 +###​注意表名要加上%pre%可以区分同一数据库中的不同的程序所生成的表 
 + 
 #表结构 #表结构
 $datainfo['​plugin_duoshuo_comment'​] = array( $datainfo['​plugin_duoshuo_comment'​] = array(
行 17: 行 20:
  $s = $zbp->​db->​sql->​CreateTable($GLOBALS['​table'​]['​plugin_duoshuo_comment'​],​$GLOBALS['​datainfo'​]['​plugin_duoshuo_comment'​]);​  $s = $zbp->​db->​sql->​CreateTable($GLOBALS['​table'​]['​plugin_duoshuo_comment'​],​$GLOBALS['​datainfo'​]['​plugin_duoshuo_comment'​]);​
  $zbp->​db->​QueryMulit($s);​  $zbp->​db->​QueryMulit($s);​
 +}
 +</​code>​
 +
 +====== 自定义数据库表的其它教程 ======
 +​提示:只验证了MySQL数据库,其余的需要自行验证。\\
 +在ZBlogPHP插件开发时,经常需要插件关联的数据表功能。\\
 +接下来以收藏文章功能,演示一下ZBlogPHP的数据库建表与增删改查。\\
 +\\
 +第一步,声明定义数据表结构。\\
 +<code php>
 +function 插件ID_defineDatabase()
 +{
 +    $database = array(
 +        /**
 +         * 收藏表
 +         */
 +        '​插件ID_Collect'​ => array(
 +            '​table' ​        => '​%pre%插件ID_collect',​
 +            '​datainfo' ​     => array(
 +                '​ID' ​           => array('​cc_ID',​ '​integer',​ '',​ 0),
 +                '​UID' ​          => array('​cc_UID',​ '​integer',​ '',​ 0),
 +                '​LogID' ​        => array('​cc_LogID',​ '​integer',​ '',​ 0),
 +                '​CreateTime' ​   => array('​cc_CreateTime',​ '​integer',​ '',​ 0),
 +                '​Meta' ​         => array('​cc_Meta',​ '​string',​ '',​ ''​),​
 +            ),
 +        ),
 +    );
 +    ​
 +    foreach ($database as $key => $v) {
 +        $table = $v['​table'​];​
 +        $datainfo = $v['​datainfo'​];​
 +        $GLOBALS['​table'​][$key] = $table;
 +        $GLOBALS['​datainfo'​][$key] = $datainfo;
 +    }
 +    ​
 +    return $database;
 +}
 +插件ID_defineDatabase();​
 +</​code>​
 +这是一个声明插件数据表结构的函数。\\
 +为什么要在定义函数以后就执行?因为需要在插件运行前,将结构信息赋值到全局变量中。\\
 +来自于c_system_base.php同类风格的声明结构,并不表示就是固定格式。\\
 +其中建议以类的名称为数据库表的声明key,方便后期的可读性。\\
 +字段“table”是数据表的名称,用%pre%+插件ID+数据表名称的方式组成。\\
 +字段“datainfo”是数据字段的结构,有具体的声明结构,可以在zb_system/​function/​lib/​sql/​中查看不同数据库的语句拼接。\\
 +关于datainfo的声明结构说明:\\
 +根据声明的顺序,数组第一条在MySQL中会加上索引(PRIMARY KEY)\\
 +声明结构  ​
 +<code php>
 +array('​字段名',​ '​类型',​ '​长度参数(根据类型定义)',​ '​默认值'​);​
 +</​code>​
 +具体的类型,大家自行补充MySQL的基础知识。\\
 +简单演示常用类型的声明:  ​
 +<code php>
 +// int
 +array('​ID',​ '​integer',​ '',​ 0),
 +
 +// tinyint
 +array('​Type',​ '​integer',​ '​tinyint',​ 0),
 +
 +// bigint
 +array('​EndTime',​ '​integer',​ '​bigint',​ 0),
 +
 +// char
 +array('​Value',​ '​char',​ 10, ''​),​
 +
 +// varchar
 +array('​Title',​ '​string',​ 250, ''​),​
 +
 +// longtext
 +array('​Content',​ '​string',​ '',​ ''​),​
 +</​code>​
 +\\
 +第二步,建表:\\
 +写一个建表的方法,在启用插件的时候执行:\\
 +<code php>
 +function 插件ID_createTable()
 +{
 +    global $zbp;
 +
 +    $database = 插件ID_defineDatabase();​
 +
 +    foreach ($database as $k => $v) {
 +        if (!$zbp->​db->​ExistTable($v['​table'​])) {
 +            $s = $zbp->​db->​sql->​CreateTable($v['​table'​],​ $v['​datainfo'​]);​
 +            $zbp->​db->​QueryMulit($s);​
 +        }
 +    }
 +}
 +
 +// 删表的方法 具体什么时候删,自行决定
 +function 插件ID_delTable()
 +{
 +    global $zbp;
 +
 +    $database = 插件ID_defineDatabase();​
 +
 +    foreach ($database as $k => $v) {
 +        if ($zbp->​db->​ExistTable($v['​table'​])) {
 +            $s = $zbp->​db->​sql->​DelTable($v['​table'​]);​
 +            $zbp->​db->​QueryMulit($s);​
 +        }
 +    }
 +}
 +</​code>​
 +在插件的启用函数中调用  ​
 +<code php>
 +function InstallPlugin_插件ID()
 +{
 +    插件ID_createTable();​
 +}
 +</​code>​
 +  ​
 +接下来声明一个类继承Base完成基本的增删改查操作。  ​
 +<code php>
 +class 插件ID_Collect extends Base
 +{
 +    /**
 +     * 在构造函数中
 +     * 将定义的数据表名称与数据结构交给父类Base去处理
 +     */
 +    public function __construct()
 +    {
 +        global $zbp;
 +
 +        parent::​__construct($zbp->​table['​插件ID_Collect'​],​ $zbp->​datainfo['​插件ID_Collect'​],​ __CLASS__);
 +        ​
 +        // 因为数据中有一条创建时间字段,提前在此赋予默认值
 +        $this->​CreateTime = time();
 +    }
 +}
 +</​code>​
 +基于这个类,就可以进行基本的curd了\\
 +新增一条数据  ​
 +<code php>
 +function 插件ID_addCollect($userId,​ $logId)
 +{
 +    $collect = new 插件ID_Collect();​
 +    $collect->​UID = $userId;
 +    $collect->​LogID = $logId;
 +    $collect->​Save();​
 +    // 保存后,$collect->​ID就能输出这条记录的ID了
 +}
 +</​code>​
 +更新一条数据  ​
 +<code php>
 +function 插件ID_updateCollect($collectId)
 +{
 +    $collect = new 插件ID_Collect();​
 +    $status = $collect->​LoadInfoByID($collectId);​
 +    if ($status) {
 +        $collect->​CreateTime = time();
 +        $collect->​Save();​
 +    }
 +    // 这样收藏的时间就变成了最新的
 +}
 +</​code>​
 +删除一条数据  ​
 +<code php>
 +function 插件ID_delCollect($collectId)
 +{
 +    $collect = new 插件ID_Collect();​
 +    $status = $collect->​LoadInfoByID($collectId);​
 +    if ($status) {
 +        $collect->​Del();​
 +    }
 +}
 +</​code>​
 +查询数据,单独阐述:\\
 +已知ID的情况,通过上面演示的LoadInfoByID($ID) 就能通过父类Base的方法读取一条数据。\\
 +在只知道UID和LogID,就需要组装where语句。\\
 +<code php>
 +function 插件ID_getUserCollect($userId,​ $logId)
 +{
 +    global $zbp;
 +    ​
 +    $w = array();
 +    $w[] = array('​=',​ '​cc_UID',​ $userId);
 +    $w[] = array('​=',​ '​cc_LogID',​ $logId);
 +    $sql = $zbp->​db->​sql->​Select($zbp->​table['​插件ID_Collect'​],​ array('​*'​),​ $w);
 +    $list = $zbp->​GetListType('​插件ID_Collect',​ $sql);
 +    if (count($list) > 0) {
 +        return $list[0];
 +    }
 +    return false;
 +}
 +</​code>​
 +其它的查询,比如查询某用户的指定数量收藏记录  ​
 +<code php>
 +function 插件ID_getUserCollectList($userId,​ $num = 10)
 +{
 +    global $zbp;
 +    ​
 +    $w = array();
 +    $w[] = array('​=',​ '​cc_UID',​ $userId);
 +    $num = (int) $num > 0 ? (int) $num : 10;
 +    $sql = $zbp->​db->​sql->​Select($zbp->​table['​插件ID_Collect'​],​ array('​*'​),​ $w, null, $num);
 +    $list = $zbp->​GetListType('​插件ID_Collect',​ $sql);
 +
 +    return $list;
 } }
 </​code>​ </​code>​
zblogphp/development/plugin/自定义数据库表.1466424118.txt · 最后更改: 2016/06/20 20:01 由 127.0.0.1