(thinkphp案例手册)(thinkphp实战案例)

上一篇文章讲解“模型-更新”,本篇文章讲解“模型-删除”。

删除为删除数据库中的一条或多条数据。

注意:

1. 以下测试都是在控制的方法中直接操作数据库的,说明控制器可以与数据库交互,但在实际项目中与数据库的交互程序都写在模型中。

2. 模型中的方法实现对数据库的操作,控制实例化模型,调用模型中的方法。

1. 删除当前模型

删除模型数据,可以在实例化后调用delete方法。

(thinkphp案例手册)(thinkphp实战案例)

①Index控制器,新建delete方法

(thinkphp案例手册)(thinkphp实战案例)

②查看未删除前的数据库

(thinkphp案例手册)(thinkphp实战案例)

③浏览器访问

(thinkphp案例手册)(thinkphp实战案例)

④查看数据库

(thinkphp案例手册)(thinkphp实战案例)

2. 根据主键删除

根据主键删除,支持单删和多删。

①Index控制器,新建idDelete方法

(thinkphp案例手册)(thinkphp实战案例)

②查看未删除前的数据库

(thinkphp案例手册)(thinkphp实战案例)

③浏览器访问

(thinkphp案例手册)(thinkphp实战案例)

④查看数据库

(thinkphp案例手册)(thinkphp实战案例)

注意:

1. 可以直接调用静态方法,例如:

User::destroy(1); 或者 User::destroy('1,2,3'); 或者 User::destroy([1,2,3]);

2. V5.0.9+版本开始当destroy方法传入空值(包括空字符串和空数组)的时候不会做任何的数据删除操作,但传入0则是有效的。

3. 条件删除

1)使用数组进行条件删除

①Index控制器,新建arrDelete方法

(thinkphp案例手册)(thinkphp实战案例)

②查看未删除前数据库

(thinkphp案例手册)(thinkphp实战案例)

③访问浏览器

(thinkphp案例手册)(thinkphp实战案例)

④查看数据库

(thinkphp案例手册)(thinkphp实战案例)

2)闭包删除

①Index控制,新建funDelete方法

(thinkphp案例手册)(thinkphp实战案例)

②查看未删除前数据库

(thinkphp案例手册)(thinkphp实战案例)

③浏览器访问

(thinkphp案例手册)(thinkphp实战案例)

④查看数据库

(thinkphp案例手册)(thinkphp实战案例)

3)通过数据库类的查询条件删除

①Index控制器,新建dbDelete方法

(thinkphp案例手册)(thinkphp实战案例)

②查看删除前的数据库

(thinkphp案例手册)(thinkphp实战案例)

③浏览器访问

(thinkphp案例手册)(thinkphp实战案例)

④查看数据库

(thinkphp案例手册)(thinkphp实战案例)

注意:

1. 以下测试都是在控制的方法中直接操作数据库的,说明控制器可以与数据库交互,但在实际项目中与数据库的交互程序都写在模型中。

2. 模型中的方法实现对数据库的操作,控制实例化模型,调用模型中的方法。

ThinkPHP5连载为卓象程序员原创,转载请联系卓象程序员

(thinkphp案例手册)(thinkphp实战案例)

关注卓象程序员,定期发布技术文章

下一篇讲解“模型-查询”

(thinkphp案例手册)(thinkphp实战案例)
(thinkphp案例手册)(thinkphp实战案例)

声明:我要去上班所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,版权归原作者程序员小娇所有,原文出处。若您的权利被侵害,请联系删除。

本文标题:(thinkphp案例手册)(thinkphp实战案例)
本文链接:https://www.51qsb.cn/article/dvjvgy.html

(0)
打赏微信扫一扫微信扫一扫QQ扫一扫QQ扫一扫
上一篇2023-08-31
下一篇2023-08-31

你可能还想知道

发表回复

登录后才能评论