本文將介紹自定義數(shù)據(jù)類型及數(shù)據(jù)庫建表和 CURD 操作步驟,接下來所用代碼需要由主程序 1.7 版本及更高版本實(shí)現(xiàn)。
一、定義數(shù)據(jù)結(jié)構(gòu)
新增數(shù)據(jù)結(jié)構(gòu)是對(duì)全局變量$table,$datainfo數(shù)組的項(xiàng)目添加:
#本篇里我們創(chuàng)建一個(gè)類叫 Custom # 表名 $table['Custom'] = '%pre%plugin_custom'; # 注意表名可自定義,但必須加上%pre%區(qū)分同一數(shù)據(jù)庫中的不同的程序所生成的表 # 表結(jié)構(gòu) $datainfo['Custom'] = array( 'ID' => array('cu_ID','integer','',0), 'Content' => array('cu_Content','string',250,''), 'LogID' => array('cu_Logid','integer','',0) ); # datainfo 數(shù)組第一項(xiàng),必須是'ID',這是表的唯一自增序列
數(shù)據(jù)表結(jié)構(gòu) $datainfo 聲明結(jié)構(gòu)如下:
array('屬性名' => array('字段名', '類型', '長度參數(shù)(根據(jù)類型定義)', '默認(rèn)值')); # 簡單演示常用類型的聲明: // int 數(shù)字 array('ID', 'integer', '', 0), // boolean 布爾值 array('Check', 'boolean', '', false), // char array('Value', 'char', 10, ''), // 最長 250 長度的字符串 array('Title', 'string', 250, ''), // 不限長的文本字符串 array('Content', 'string', '', ''),
二、創(chuàng)建數(shù)據(jù)類型
自定義數(shù)據(jù)類型是創(chuàng)建一個(gè)繼承自系統(tǒng)的Base類的新類:
#這里定義了一個(gè)最基本的Custom類,可以在這個(gè)類里擴(kuò)展自己的方法 class Custom extends Base { public function __construct() { global $zbp; parent::__construct($zbp->table['Custom'], $zbp->datainfo['Custom'], __CLASS__); } }
三、創(chuàng)建數(shù)據(jù)表
創(chuàng)建數(shù)據(jù)庫的數(shù)據(jù)表,在應(yīng)用的include.php頁面里的安裝函數(shù)進(jìn)行數(shù)據(jù)庫表的存在判斷及創(chuàng)建:
function InstallPlugin_應(yīng)用ID() { global $zbp; # 判斷是否已創(chuàng)建,否則新建數(shù)據(jù)表 if(!$zbp->db->ExistTable($zbp->table['Custom'])) { $s = $zbp->db->sql->CreateTable($zbp->table['Custom'], $zbp->datainfo['Custom']); $zbp->db->QueryMulit($s); } } # PS:應(yīng)用卸載時(shí)可根據(jù)自身需求來刪除數(shù)據(jù)表或保留所創(chuàng)建的數(shù)據(jù)表
四、刪除數(shù)據(jù)表
刪除數(shù)據(jù)庫中數(shù)據(jù)表的方法 具體什么時(shí)候刪,自行決定:
function 插件ID_delTable() { global $zbp; if ($zbp->db->ExistTable($zbp->table['Custom'])) { $s = $zbp->db->sql->DelTable($zbp->table['Custom']); $zbp->db->QueryMulit($s); } } # PS:應(yīng)用卸載時(shí)可根據(jù)自身需求來刪除數(shù)據(jù)表或保留所創(chuàng)建的數(shù)據(jù)表
五、自定義CURD操作
1、新增一條數(shù)據(jù)
$c = new Custom(); $c->Content = 'abc'; $c->LogID = 123; $c->Save(); echo $c->ID; //輸出 新插入的對(duì)象的ID值
2、更新一條數(shù)據(jù)
$c = new Custom(); if ($c->LoadInfoByID(1) == true) { $c->Content = '12345'; $c->Save(); }
3、刪除一條數(shù)據(jù)
$c = new Custom(); if ($c->LoadInfoByID(2) == true) { $c->Del(); }
4、查詢單條數(shù)據(jù)
$c = new Custom(); $c->LoadInfoByID(123); #LoadInfoByID返回值為false即加載不成功
5、查詢多條數(shù)據(jù)
#查詢Custom表下符合2個(gè)條件的所有記錄 $w[] = array('=', $zbp->d['Custom']['LogID'][0], 123);//查詢條件1,LogID值為123 $w[] = array('=', $zbp->d['Custom']['Content'][0], 'abc');//查詢條件2,Content值為abc $sql = $zbp->db->sql->Select($zbp->t['Custom'], array('*'), $w); $list = $zbp->GetListType('Custom', $sql); #結(jié)果$list為一個(gè)包含Custom對(duì)象的數(shù)組
主程序 1.7.2 版新增的查詢方法:
主程序 1.7.2 在定義Custom類成功后,自動(dòng)生成了ZBlogPHP類下的 3 個(gè)讀取加載Custom類的方法,分別是:
- GetCustomList($select = null, $where = null, $order = null, $limit = null, $option = null)
#查詢Custom表下符合2個(gè)條件的所有記錄 //本次使用zbp的鏈?zhǔn)絊QL操作組件 $sql = $zbp->db->sql->get() ->select($zbp->table['Custom']) ->where(array('=', 'cu_Logid', '123'))//查詢條件1,LogID為123 ->where(array('=', 'cu_Content', 'abc'))//查詢條件2,Content值為abc ->orderBy(array('cu_ID' => 'desc'))//排序按ID序號(hào)的倒序 ->sql; $list = $zbp->GetCustomList($sql); #如果取不到數(shù)據(jù)$list就返回一個(gè)空array()
- GetCustomByID($id)
#從數(shù)據(jù)庫中讀取單個(gè)的Custom實(shí)例 $c = $zbp->GetCustomByID(111); #讀取成功返回實(shí)例,不成功返回null
- GetCustomByArray($array, $field_name = ‘ID’)
#從數(shù)據(jù)庫中查詢ID值等于給定數(shù)組項(xiàng)目的Custom實(shí)例隊(duì)列 #如果是查詢ID值,$field_name參數(shù)可以省略 $list = $zbp->GetCustomByArray(array(1,3,5,666)); #返回隊(duì)列為一個(gè)Custom實(shí)例的數(shù)組(ID分別為1,3,5,666,如果ID存在的話)
介紹GetCustomByArray的一個(gè)隱藏的復(fù)雜用法:
#設(shè)有一個(gè)posts文章數(shù)組,內(nèi)有3個(gè)post實(shí)例對(duì)象,ID分別為1,2,13 $posts = array( $zbp->GetPostByID(1), $zbp->GetPostByID(2), $zbp->GetPostByID(13), ); #查詢Custom數(shù)據(jù)表中LogID值等于posts里post實(shí)例ID值的Custom實(shí)例隊(duì)列 $list = $zbp->GetCustomByArray($posts, array('ID' => 'LogID')); #返回隊(duì)列為一個(gè)Custom實(shí)例的數(shù)組,它們的LogID都是1或2或13