ThinkPHP6 数据库

901次阅读
没有评论
  • ThinkPHP6 数据库和模型操作已经独立为 ThinkORM 库
  • 要使用 Db 类必须使用门面方式( think\facade\Db )调用
  • 数据库操作统一入口: Db::

一、数据库管理软件

1,phpMyAdmin(网页数据库管理)
2,Navicat for MySql(windows 软件数据库管理)


二、创建数据库

  • 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 '状态 1 开启 2 关闭', `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 '状态 1 开启 2 关闭', 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, '孕产', 1); INSERT INTO `shop_cat` VALUES (4, '童装', 1); INSERT INTO `shop_cat` VALUES (5, '电视', 1); INSERT INTO `shop_cat` VALUES (6, '手机', 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 '商品标题', `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 '状态 1 开启 2 关闭 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, '云朵般轻盈的仙女裙 高级钉珠收腰长裙 气质无袖连衣裙', 279.99, 0, 1100, 1, 1576080000); INSERT INTO `shop_goods` VALUES (2, 1, '高冷御姐风灯芯绒 a 字连衣裙女秋冬 2019 年新款收腰显瘦复古裙子', 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 '状态 1 开启 2 关闭', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COMMENT='左侧菜单表'; 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, '左侧菜单', 9, 1);

三、执行原生  MySql

1、query  方法用于执行  MySql  查询操作

public function index(){ $query = Db::query("SELECT * FROM `shop_goods` where status=1"); print_r($query); }

2、execute  方法用于执行  MySql  新增和修改操作

public function index(){ $execute = Db::execute("INSERT INTO `shop_goods` VALUES (3, 1,'2019 秋冬新款时尚简约纯羊绒加厚圆领羊绒长裙显瘦气质连衣裙女 ', 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、单条数据查询  find

  • find  方法查询结果不存在,返回  null,否则返回结果数组
public function index(){ $find = Db::table('shop_goods')->find(5); print_r($find); }

2、多条数据查询  select

  • select  方法查询结果是一个二维数组,如果结果不存在,返回空数组
public function index(){ $select = Db::table('shop_goods')->select(); print_r($select); }

3、查询某个字段的值  value

  • value  方法查询结果不存在,返回 null
public function index(){ $value = Db::table('shop_goods')->value('title'); print_r($value); }

4、查询某一列的值  column

  • column  方法查询结果不存在,返回空数组
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、添加一条数据  insert

  • insert  方法添加数据成功返回添加成功的条数,通常情况返回 1
public function index(){ $data = ['cat'=>'1','title'=>'日系小浪漫与温暖羊毛针织拼接网纱百褶中长收腰连衣裙','price'=>'1598.35','add_time'=>1576080000]; $insert = Db::table('shop_goods')->insert($data); print_r($insert); }

2、添加一条数据  insertGetId

  • insertGetId  方法添加数据成功返回添加数据的自增主键
public function index(){ $data = ['cat'=>'1','title'=>'针织毛衣连衣裙 2019 秋冬新款气质宽松羊毛长袖中长款休闲打底裙女','price'=>'690.00','add_time'=>1576080000]; $insert = Db::table('shop_goods')->insertGetId($data); print_r($insert); }

3、添加多条数据  insertAll

  • insertAll  方法添加数据成功返回添加成功的条数
public function index(){ $data = [ ['cat'=>'1','title'=>'秋冬加厚连衣裙女超长款宽松羊绒衫高领套头过膝毛衣百搭针织长裙','price'=>'658.00','add_time'=>1576080000], ['cat'=>'1','title'=>'2019 新款秋冬慵懒风宽松毛衣针织连衣裙复古港味网红两件套','price'=>'408.00','add_time'=>1576080000], ['cat'=>'2','title'=>'男士长袖 t 恤秋季圆领黑白体恤 T 纯色上衣服打底衫男装','price'=>'99.00','add_time'=>1576080000] ]; $insert = Db::table('shop_goods')->insertAll($data); print_r($insert); }

六、修改

1、修改数据  update

  • update  方法返回影响数据的条数,没修改任何数据返回 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、删除数据  delete

  • delete  方法返回影响数据的条数,没有删除返回 0
public function index(){ # 根据条件删除数据 $delete = Db::table('shop_goods')->where('id',1)->delete(); print_r($delete); # 删除主键为 2 的数据 $delete = Db::table('shop_goods')->delete(2); print_r($delete); # 删除整表数据 $delete = Db::table('shop_goods')->delete(true); print_r($delete); }

2、软删除  useSoftDelete

  • 业务数据不建议真实删除数据,TP 系统提供了软删除机制
public function index(){ # 软删除 $delete = Db::table('shop_goods')->useSoftDelete('status',3)->delete(); print_r($delete); }

八、其他操作(自学)

  • save  方法统一写入数据,自动判断是新增还是更新数据(以写入数据中是否存在主键数据为依据)。
public function index(){ # 添加数据 $data = ['cat'=>'2','title'=>'美特斯邦威七分牛仔裤女 2018 夏季新款中腰修身洗水牛仔裤商场款','price'=>'49.90','add_time'=>1576080000]; $save = Db::table('shop_goods')->save($data); print_r($save); # 修改数据 $data = ['price'=>'99.00','id'=>3]; $save = Db::table('shop_goods')->save($data); print_r($save); }

备注:增删查改是常规操作


九、数据集

  • Thinkphp 提供了很多处理数据集的方法
方法 描述
toArray 转换为数组
isEmpty 是否为空
all 所有数据
merge 合并其它数据
diff 比较数组,返回差集
flip 交换数据中的键和值
intersect 比较数组,返回交集
keys 返回数据中的所有键名
pop 删除数据中的最后一个元素
shift 删除数据中的第一个元素
unshift 在数据开头插入一个元素
push 在结尾插入一个元素
reduce 通过使用用户自定义函数,以字符串返回数组
reverse 数据倒序重排
chunk 数据分隔为多个数据块
each 给数据的每个元素执行回调
filter 用回调函数过滤数据中的元素
column 返回数据中的指定列
sort 对数据排序
order 指定字段排序
shuffle 将数据打乱
slice 截取数据中的一部分
map 用回调函数处理数组中的元素
where 根据字段条件过滤数组中的元素
whereLike Like 查询过滤元素
whereNotLike Not Like 过滤元素
whereIn IN 查询过滤数组中的元素
whereNotIn Not IN 查询过滤数组中的元素
whereBetween Between 查询过滤数组中的元素
whereNotBetween Not Between 查询过滤数组中的元素
public function index(){ $select = Db::table('shop_goods')->select(); if($select->isEmpty()){ echo '未找到数据'; } 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> 商品标题 </th> <th> 分类 </th> <th> 原价 </th> <th> 折扣 </th> <th> 现价 </th> <th> 库存 </th> <th> 状态 </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/} 关闭 {/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" /}
正文完
 0
飞翔的mouse
版权声明:本站原创文章,由 飞翔的mouse 于2020-03-28发表,共计8226字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。