ThinkPHP6.0 數(shù)據(jù)庫
ThinkPHP6 數(shù)據(jù)庫和模型操作已經(jīng)獨(dú)立為ThinkORM庫.
ThinkPHP6 數(shù)據(jù)庫
ThinkPHP6 數(shù)據(jù)庫和模型操作已經(jīng)獨(dú)立為
ThinkORM庫
要使用Db類必須使用門面方式(
think\facade\Db
)調(diào)用數(shù)據(jù)庫操作統(tǒng)一入口:
Db::
一、數(shù)據(jù)庫管理軟件
1,phpMyAdmin(網(wǎng)頁數(shù)據(jù)庫管理)
2,Navicat for MySql(windows軟件數(shù)據(jù)庫管理)
二、創(chuàng)建數(shù)據(jù)庫
1、管理員表
CREATE TABLE `shop_admin` (
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用戶ID',
`account` varchar(50) NOT NULL COMMENT '賬戶',
`password` char(32) NOT NULL COMMENT '密碼',
`name` varchar(50) NOT NULL COMMENT '姓名',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '狀態(tài) 1開啟 2關(guān)閉',
`add_time` int(10) unsigned NOT NULL COMMENT '添加時間',
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='后臺管理員';
INSERT INTO `shop_admin` VALUES (1, 'ouyangke', 'e10adc3949ba59abbe56e057f20f883e', '歐陽克', 1, 1576080000);
2、商品分類表
DROP TABLE IF EXISTS `shop_cat`;
CREATE TABLE `shop_cat` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(50) NOT NULL COMMENT '分類名',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '狀態(tài) 1開啟 2關(guān)閉',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COMMENT='分類表';
INSERT INTO `shop_cat` VALUES (1, '女裝', 1);
INSERT INTO `shop_cat` VALUES (2, '男裝', 1);
INSERT INTO `shop_cat` VALUES (3, '孕產(chǎn)', 1);
INSERT INTO `shop_cat` VALUES (4, '童裝', 1);
INSERT INTO `shop_cat` VALUES (5, '電視', 1);
INSERT INTO `shop_cat` VALUES (6, '手機(jī)', 1);
INSERT INTO `shop_cat` VALUES (7, '電腦', 1);
3、商品表
CREATE TABLE `shop_goods` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT ' 商品ID',
`cat` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '分類ID',
`title` varchar(200) NOT NULL COMMENT '商品標(biāo)題',
`price` double(10,2) unsigned NOT NULL COMMENT '價格',
`discount` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '折扣',
`stock` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '庫存',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '狀態(tài) 1開啟 2關(guān)閉 3刪除',
`add_time` int(10) unsigned NOT NULL COMMENT '添加時間',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
INSERT INTO `shop_goods` VALUES (1, 1, '云朵般輕盈的仙女裙 高級釘珠收腰長裙 氣質(zhì)無袖連衣裙', 279.99, 0, 1100, 1, 1576080000);
INSERT INTO `shop_goods` VALUES (2, 1, '高冷御姐風(fēng)燈芯絨a字連衣裙女秋冬2019年新款收腰顯瘦復(fù)古裙子', 255.90, 0, 100, 1, 1576080000);
4、菜單表
CREATE TABLE `shop_menu` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`title` varchar(50) NOT NULL COMMENT '菜單名',
`fid` int(10) NOT NULL COMMENT '父ID',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '狀態(tài) 1開啟 2關(guān)閉',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COMMENT='左側(cè)菜單表';
INSERT INTO `shop_menu` VALUES (1, '商品管理', 0, 1);
INSERT INTO `shop_menu` VALUES (2, '商品列表', 1, 1);
INSERT INTO `shop_menu` VALUES (3, '商品分類', 1, 1);
INSERT INTO `shop_menu` VALUES (4, '用戶管理', 0, 1);
INSERT INTO `shop_menu` VALUES (5, '用戶列表', 4, 1);
INSERT INTO `shop_menu` VALUES (6, '購物車', 4, 1);
INSERT INTO `shop_menu` VALUES (7, '用戶地址', 4, 1);
INSERT INTO `shop_menu` VALUES (8, '訂單管理', 4, 1);
INSERT INTO `shop_menu` VALUES (9, '后臺管理', 0, 1);
INSERT INTO `shop_menu` VALUES (10, '管理員列表', 9, 1);
INSERT INTO `shop_menu` VALUES (11, '個人中心', 9, 1);
INSERT INTO `shop_menu` VALUES (12, '左側(cè)菜單', 9, 1);
三、執(zhí)行原生 MySql
1、query
方法用于執(zhí)行 MySql 查詢操作
public function index(){
$query = Db::query("SELECT * FROM `shop_goods` where status=1");
print_r($query);
}
2、execute
方法用于執(zhí)行 MySql 新增和修改操作
public function index(){
$execute = Db::execute("INSERT INTO `shop_goods` VALUES (3, 1, '2019秋冬新款時尚簡約純羊絨加厚圓領(lǐng)羊絨長裙顯瘦氣質(zhì)連衣裙女', 1179.00, 0, 200, 1, 1576080000)");
print_r($execute);
$execute = Db::execute("UPDATE `shop_goods` set `price`='1100' where `id`=3 ");
print_r($execute);
}
四、查詢
1、單條數(shù)據(jù)查詢 find
find
方法查詢結(jié)果不存在,返回 null,否則返回結(jié)果數(shù)組
public function index(){
$find = Db::table('shop_goods')->find(5);
print_r($find);
}
2、多條數(shù)據(jù)查詢 select
select
方法查詢結(jié)果是一個二維數(shù)組,如果結(jié)果不存在,返回空數(shù)組
public function index(){
$select = Db::table('shop_goods')->select();
print_r($select);
}
3、查詢某個字段的值 value
value
方法查詢結(jié)果不存在,返回 null
public function index(){
$value = Db::table('shop_goods')->value('title');
print_r($value);
}
4、查詢某一列的值 column
column
方法查詢結(jié)果不存在,返回空數(shù)組
public function index(){
$column = Db::table('shop_goods')->column('title');
print_r($column);
$column = Db::table('shop_goods')->column('title','id');
print_r($column);
}
五、添加
1、添加一條數(shù)據(jù) insert
insert
方法添加數(shù)據(jù)成功返回添加成功的條數(shù),通常情況返回 1
public function index(){
$data = ['cat'=>'1','title'=>'日系小浪漫與溫暖羊毛針織拼接網(wǎng)紗百褶中長收腰連衣裙','price'=>'1598.35','add_time'=>1576080000];
$insert = Db::table('shop_goods')->insert($data);
print_r($insert);
}
2、添加一條數(shù)據(jù) insertGetId
insertGetId
方法添加數(shù)據(jù)成功返回添加數(shù)據(jù)的自增主鍵
public function index(){
$data = ['cat'=>'1','title'=>'針織毛衣連衣裙2019秋冬新款氣質(zhì)寬松羊毛長袖中長款休閑打底裙女','price'=>'690.00','add_time'=>1576080000];
$insert = Db::table('shop_goods')->insertGetId($data);
print_r($insert);
}
3、添加多條數(shù)據(jù) insertAll
insertAll
方法添加數(shù)據(jù)成功返回添加成功的條數(shù)
public function index(){
$data = [
['cat'=>'1','title'=>'秋冬加厚連衣裙女超長款寬松羊絨衫高領(lǐng)套頭過膝毛衣百搭針織長裙','price'=>'658.00','add_time'=>1576080000],
['cat'=>'1','title'=>'2019新款秋冬慵懶風(fēng)寬松毛衣針織連衣裙復(fù)古港味網(wǎng)紅兩件套','price'=>'408.00','add_time'=>1576080000],
['cat'=>'2','title'=>'男士長袖t恤秋季圓領(lǐng)黑白體恤T 純色上衣服打底衫男裝','price'=>'99.00','add_time'=>1576080000]
];
$insert = Db::table('shop_goods')->insertAll($data);
print_r($insert);
}
六、修改
1、修改數(shù)據(jù) update
update
方法返回影響數(shù)據(jù)的條數(shù),沒修改任何數(shù)據(jù)返回 0
public function index(){
$data = ['price'=>'68'];
$update = Db::table('shop_goods')->where('id',8)->update($data);
print_r($update);
}
2、自增 inc
inc
方法自增一個字段的值
public function index(){
$inc = Db::table('shop_goods')->where('id',5)->inc('stock')->update();
print_r($inc);
# 字段的值增加5
$inc = Db::table('shop_goods')->where('id',6)->inc('stock',5)->update();
print_r($inc);
}
3、自減 dec
dec
方法自減一個字段的值
public function index(){
# 字段的值減去1
$dec = Db::table('shop_goods')->where('id',7)->dec('stock')->update();
print_r($dec);
# 字段的值減去5
$dec = Db::table('shop_goods')->where('id',8)->dec('stock',5)->update();
print_r($dec);
}
七、刪除
1、刪除數(shù)據(jù) delete
delete
方法返回影響數(shù)據(jù)的條數(shù),沒有刪除返回 0
public function index(){
# 根據(jù)條件刪除數(shù)據(jù)
$delete = Db::table('shop_goods')->where('id',1)->delete();
print_r($delete);
# 刪除主鍵為2的數(shù)據(jù)
$delete = Db::table('shop_goods')->delete(2);
print_r($delete);
# 刪除整表數(shù)據(jù)
$delete = Db::table('shop_goods')->delete(true);
print_r($delete);
}
2、軟刪除 useSoftDelete
業(yè)務(wù)數(shù)據(jù)不建議真實(shí)刪除數(shù)據(jù),TP系統(tǒng)提供了軟刪除機(jī)制
public function index(){
# 軟刪除
$delete = Db::table('shop_goods')->useSoftDelete('status',3)->delete();
print_r($delete);
}
八、其他操作(自學(xué))
save
方法統(tǒng)一寫入數(shù)據(jù),自動判斷是新增還是更新數(shù)據(jù)(以寫入數(shù)據(jù)中是否存在主鍵數(shù)據(jù)為依據(jù))。
public function index(){
# 添加數(shù)據(jù)
$data = ['cat'=>'2','title'=>'美特斯邦威七分牛仔褲女2018夏季新款中腰修身洗水牛仔褲商場款','price'=>'49.90','add_time'=>1576080000];
$save = Db::table('shop_goods')->save($data);
print_r($save);
# 修改數(shù)據(jù)
$data = ['price'=>'99.00','id'=>3];
$save = Db::table('shop_goods')->save($data);
print_r($save);
}
備注:增刪查改是常規(guī)操作
九、數(shù)據(jù)集
Thinkphp提供了很多處理數(shù)據(jù)集的方法
方法 | 描述 |
toArray | 轉(zhuǎn)換為數(shù)組 |
isEmpty | 是否為空 |
all | 所有數(shù)據(jù) |
merge | 合并其它數(shù)據(jù) |
diff | 比較數(shù)組,返回差集 |
flip | 交換數(shù)據(jù)中的鍵和值 |
intersect | 比較數(shù)組,返回交集 |
keys | 返回數(shù)據(jù)中的所有鍵名 |
pop | 刪除數(shù)據(jù)中的最后一個元素 |
shift | 刪除數(shù)據(jù)中的第一個元素 |
unshift | 在數(shù)據(jù)開頭插入一個元素 |
push | 在結(jié)尾插入一個元素 |
reduce | 通過使用用戶自定義函數(shù),以字符串返回數(shù)組 |
reverse | 數(shù)據(jù)倒序重排 |
chunk | 數(shù)據(jù)分隔為多個數(shù)據(jù)塊 |
each | 給數(shù)據(jù)的每個元素執(zhí)行回調(diào) |
filter | 用回調(diào)函數(shù)過濾數(shù)據(jù)中的元素 |
column | 返回數(shù)據(jù)中的指定列 |
sort | 對數(shù)據(jù)排序 |
order | 指定字段排序 |
shuffle | 將數(shù)據(jù)打亂 |
slice | 截取數(shù)據(jù)中的一部分 |
map | 用回調(diào)函數(shù)處理數(shù)組中的元素 |
where | 根據(jù)字段條件過濾數(shù)組中的元素 |
whereLike | Like查詢過濾元素 |
whereNotLike | Not Like過濾元素 |
whereIn | IN查詢過濾數(shù)組中的元素 |
whereNotIn | Not IN查詢過濾數(shù)組中的元素 |
whereBetween | Between查詢過濾數(shù)組中的元素 |
whereNotBetween | Not Between查詢過濾數(shù)組中的元素 |
public function index(){
$select = Db::table('shop_goods')->select();
if($select->isEmpty()){
echo '未找到數(shù)據(jù)';
}
print_r($select->toArray());
}
十、示例
controller代碼
namespace app\controller;
use think\facade\View;
class Index{
public function index(){
$title = '商城';
$login = '歐陽克';
$menu = Db::table('shop_menu')->where('fid',0)->select();
$left = $menu->toArray();
foreach($left as &$left_v){
$left_v['lists'] = Db::table('shop_menu')->where('fid',$left_v['id'])->select();
}
$list = Db::table('shop_goods')->select();
$right = $list->toArray();
foreach($right as &$right_v){
$right_v['cat'] = Db::table('shop_cat')->where('id',$right_v['cat'])->value('name');
}
View::assign([
'title' => $title,
'login' => $login,
'left' => $left,
'right' => $right
]);
return View::fetch();
}
}
view代碼:index.html
{include file="public/head" /}
{include file="public/left" /}
<div class="main" style="padding:10px;">
<div class="content">
<span>商品列表</span>
<button class="layui-btn layui-btn-sm" onclick="add()">添加</button>
<div></div>
</div>
<table class="layui-table">
<thead>
<tr>
<th>ID</th>
<th>商品標(biāo)題</th>
<th>分類</th>
<th>原價</th>
<th>折扣</th>
<th>現(xiàn)價</th>
<th>庫存</th>
<th>狀態(tài)</th>
<th>添加時間</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name="right" id="right_v"}
<tr>
<td>{$right_v.id}</td>
<td>{$right_v.title}</td>
<td>{$right_v.cat}</td>
<td>{$right_v.price}</td>
<td>{$right_v.discount}</td>
<td>
{if $right_v.discount!=0}
{$right_v.price*($right_v.discount/10)}
{else/}
{$right_v.price}
{/if}
</td>
<td>{$right_v.stock}</td>
<td>{if $right_v['status']==1}開啟{else/}關(guān)閉{/if}</td>
<td>{$right_v.add_time|date='Y-m-d'}</td>
<td>
<button class="layui-btn layui-btn-xs" onclick="edit()">編輯</button>
<button class="layui-btn layui-btn-danger layui-btn-xs" onclick="del()">刪除</button>
</td>
</tr>
{/volist}
</tbody>
</table>
</div>
{include file="public/bottom" /}