一、安装Composer
第一步:双击下载好的composeri运行程序
第二步:选择要安装的盘符
第三步:选择php版本。如果你是集成包环境,就到集成包里找php
第四步:全部下一步
二、设置Composer下载源
先设置composer的下载源,也是镜像地址
在命令行窗口或控制台输入aliyun的镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
三、Composer下载,安装TinkPHP6
先切换到php环镜根目录
cd D:/phpEnv/www
执行下载Thinkphpe6命令。最后的tp,是新建个p目录,可更改
composer create-project topthink/think tp
如果要更新的话执行下面命令即可,不能在www里运行,在tp6文件夹里运行更新命令
composer update topthink/framework
四、安装成功后 目录结构
6.0版本目录结构的主要变化是核心框架纳入vendor目录,然后原来的application目录变成app目录。
安装后的目录结构就是一个单应用模式
在mac或者1inux环境下面,注意需要设置runtime目录权限为777

五、运行
·注:咱们只讲windows系统里的php环境集成包,如何访问
第一步:打开phpstudy集成软件-》站点域名管理
第二步:网站域名:www.xxx.com
第三步:网站目录:tp/public
第四步:在C:\Windows\System32 drivers\etc目录下,打开hosts文件
第五步:最后一行输入127.0.0.1www.Xx.com
第六步:直接在览器上输入域名(wwx.com)
ThinkPHP6 起步
ThinkPHP支持传统的MVC(Model-.VMew-Controller)模式以及流行的MVWM(Model–VMew-ViewModel)模式的应用
开发教程里面我们以mvc为例子讲解
一、MVC
mvc软件系统分为三个基本部分:模型(Model)、视图(Vew)和控制器(Controller)
ThinkPHP6是一个典型的MvC架构
控制器-控制器,用于将用户请求转发给相应的Model进行处理,并根据Modd的计算结果向用户提供相应响应
视图·为用户提供使用界面,与用户直接进行交互。
模型,承载数据,并对用户提交请求进行计算的模块。
MVC架构程序的工作流程:
(1)用户通过View页面向服务端提出请求,可以是表单请求、超链接请求、AJAX请求等
(2)服务端Controller控制器接收到请求后对请求进行解析,找到相应的Model对用户请求进行处理
(3)Model处理后,将处理结果再交给Controller
(4)Controller在接到处理结果后,根据处理结果找到要作为向客户端发回的响应VMew页面。页面经這染(数据填
充)后,再发送给客户端。
模型:数据库增删改查、算法等
控制器:view和model的桥梁

二、单应用模式访问

入口文件是:


- controller下的php文件只能有一个class类,class类名必须和文件名保持一致
调试
显示错误信息,改成true即可
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OkRk342H-1685785920770)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530183223122.png)]](https://i-blog.csdnimg.cn/blog_migrate/372ac2b83de9b3e52e72abf7ee076a14.png)
三、安装视图
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8O2Lma35-1685785920771)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530183524920.png)]](https://i-blog.csdnimg.cn/blog_migrate/96caa253eee390a2d711be1a5344936a.png)
composer require topthink/think-view
需要在cmd中进入tp文件夹里面执行命令
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oWC49lhV-1685785920771)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530183749427.png)]](https://i-blog.csdnimg.cn/blog_migrate/5e7ccae98a4243539b242b73a788e57b.png)
默认view(需手动创建)和control目录同级,若要修改config/view.php


四、模板渲染
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3ccGYlrY-1685785920772)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530184340830.png)]](https://i-blog.csdnimg.cn/blog_migrate/af856707a108f59c3fb1f327a430ddaf.png)
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
return view::fetch();//默认访问controller的目录view/index(控制器or类)/index(方法).html
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function login(){
return 'login';
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-erB160m8-1685785920772)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530185712793.png)]](https://i-blog.csdnimg.cn/blog_migrate/a42f644762e5c602ff7dd56d1234f6e5.png)
默认访问
view::fetch() 默认访问view/当前控制器名的小写(即index)/index.html(方法名.html)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YycWrtsu-1685785920772)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530184634002.png)]](https://i-blog.csdnimg.cn/blog_migrate/8f5bbb0e0a82183982e0dc2e3243c622.png)
指定访问
将view::fetch()指定参数index/index,访问view/index/index.html

![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kuBdmPJJ-1685785920772)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530185530456.png)]](https://i-blog.csdnimg.cn/blog_migrate/068cd315932cfa54e7eea8174865c4d1.png)
五、模板变量
默认赋值
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XAz9l0eR-1685785920773)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530185844092.png)]](https://i-blog.csdnimg.cn/blog_migrate/83dac4d4811efcf334049cdfe24b9eb8.png)
View::assign(a,b) 表示键值对,a变量即传入前端view中的变量,b是a的取值
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
name:{$name}
<br/>
qq:{$qq}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KMGbFyMM-1685785920773)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530190837992.png)]](https://i-blog.csdnimg.cn/blog_migrate/89328a2fbbc2aa3d0061b3497c1e8141.png)
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;
class Index extends BaseController
{
public function index()
{
return view::fetch('index/index',[
'name'=>'1',
'qq'=>'2'
]);
}
}
View::fetch()中,index/index表示需要渲染的页面,第二个即数组变量
助手函数(若不使用默认赋值的话)
不建议使用助手函数,效率比上述方法低
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
return view('index/index',[
'name'=>'1',
'qq'=>'2'
]);// 其中index/index可以指定
}
}
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
name:{$name}
<br/>
qq:{$qq}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-N04i5TiX-1685785920773)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530190616389.png)]](https://i-blog.csdnimg.cn/blog_migrate/d7903da88f395bf848dfd73a0de47569.png)
ThinkPHP6模板引擎
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0dh9meXF-1685785920773)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530191025613.png)]](https://i-blog.csdnimg.cn/blog_migrate/8aa37c88ce79475136b74f2015a36013.png)
一、运算符
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cF3zJgJl-1685785920774)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530191038508.png)]](https://i-blog.csdnimg.cn/blog_migrate/121d7311edce863a88dd8e81bd1d2b33.png)
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
return view('index/index',[
'a'=>2,
'b'=>3
]);// 其中index/index可以指定
}
}
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
a+b={$a+$b}
<!-- 若{和$ 之间有空格,则无法进行运算:a+b={ $a+$b}-->
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YtGc63DU-1685785920774)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530191516312.png)]](https://i-blog.csdnimg.cn/blog_migrate/9ef1261daec0402c2b058505fce2ef15.png)
二、模板函数

![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w4BeFpDA-1685785920774)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530191610521.png)]](https://i-blog.csdnimg.cn/blog_migrate/26660117ed680959277102d1fe305d44.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PggZCG91-1685785920775)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530191656642.png)]](https://i-blog.csdnimg.cn/blog_migrate/608a419726ee9f74e38bd38eafdfd951.png)

- default函数,若$default 变量存在则显示default变量,若不存在则显示default="小鱼"这个默认值
- substr=0,3 表示截取前3个字符
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w9Gv4OU7-1685785920775)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530192113007.png)]](https://i-blog.csdnimg.cn/blog_migrate/310f4ec040ca836a87c45fc88bde1903.png)
模板注释阻止编译,html注释无法阻止编译,即前端注释可以在前端通过查看源码看到,而模板注释看不到
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7bfivDz1-1685785920775)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530192258079.png)]](https://i-blog.csdnimg.cn/blog_migrate/ce72445dac0e3de419bdb4a9916a4565.png)
原样输出
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
return view('index/index',[
'a'=>2,
'b'=>3
]);// 其中index/index可以指定
}
}
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
a+b={$a+$b}
<br/>
{literal}
a+b={$a+$b}
{/literal}
</body>
</html>
literal内不进行编译
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K2T0uFsf-1685785920775)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530192534142.png)]](https://i-blog.csdnimg.cn/blog_migrate/7710c6b64560b9bcb43548c84f431e46.png)
html内编译php
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{php}
echo "小鱼";
{/php}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cui5wS4N-1685785920775)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530200132322.png)]](https://i-blog.csdnimg.cn/blog_migrate/5528596025a2ab13e12d5435f64a6cfb.png)
三、循环标签
foreach 循环
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
$arr=[
[
'id'=>1,
'name'=>'小鱼'
],
[
'id'=>2,
'name'=>'小夏'
]
];
View::assign('arr',$arr);
return View::fetch();
}
}
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--使用每一项进行输出-->
{foreach $arr as $item}
<div>
<span>id:{$item['id']}</span>
<span>name:{$item['name']}</span>
</div>
{/foreach}
<!--或者采用 索引(从0开始) => item 键值对-->
{foreach $arr as $index=>$item}
<div>
<span>{$index} id:{$item['id']}</span>
<span>{$index} name:{$item['name']}</span>
</div>
{/foreach}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vNYo5bpc-1685785920776)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530193535529.png)]](https://i-blog.csdnimg.cn/blog_migrate/fc95a19e0d66e97eca7fdfb4f1375053.png)
volist 循环
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8153jrzE-1685785920776)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530193731483.png)]
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
$arr=[
[
'id'=>1,
'name'=>'小鱼'
],
[
'id'=>2,
'name'=>'小夏'
]
];
View::assign('arr',$arr);
return View::fetch();
}
}
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--使用每一项进行输出,name=数组名,id=每一项的变量名,key=下标从1开始,offset=偏移量(若=1,则原本第一个不输出),length=输出长度(前n条)-->
{volist name="arr" id="item" key="k" offset="1" length="1"}
<div>
<span>{$K} id:{$item['id']}</span>
<span>{$K} name:{$item['name']}</span>
</div>
{/volist}
</body>
</html>
for循环
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TIZmKp2S-1685785920776)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530194408943.png)\
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--for 迭代,输出[1,10),不包括10,相当于python中的range()-->
{for start="1" end="10" }
{$i}
<br/>
{/for}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JLVLXbri-1685785920776)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530194618361.png)]](https://i-blog.csdnimg.cn/blog_migrate/4904519f9fec47e9b836079a46ec2c34.png)
四 判断标签
if判断
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
class Index extends BaseController
{
public function index()
{
View::assign('status',1);
View::assign('week',3);
return View::fetch();
}
}
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{if $status == 1}
/if标签表示结束<br/>
{/if}
{if $week==1}
{elseif $week==3/}
elseif和else结合的判断方法
{else /}
{/if}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BwhSxcSy-1685785920777)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530195340358.png)]](https://i-blog.csdnimg.cn/blog_migrate/6beb3c48ff9f067fb5af138a396cd825.png)
switch判断
app/view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--switch不需要break,会自动break-->
{switch $week}
{case 0}<div>星期日</div>{/case}
{case 3}<div>星期三</div>{/case}
{/switch}
</body>
</html>
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eLG794Y7-1685785920777)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530195842434.png)]](https://i-blog.csdnimg.cn/blog_migrate/4d764363c7906baf71a109a266d3959d.png)
条件标签
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2oZvAjby-1685785920777)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530194931499.png)]](https://i-blog.csdnimg.cn/blog_migrate/893b4a97e8c884a783d5548b94935209.png)
比较标签
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pHBaeRdS-1685785920777)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530194947830.png)]](https://i-blog.csdnimg.cn/blog_migrate/16da989639ac934b7d8506317f65c63b.png)
ThinkPHP6数据库
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7rJDMPOu-1685785920777)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530200306269.png)]](https://i-blog.csdnimg.cn/blog_migrate/830e922d4f1ffb466b2283f2a7337fdc.png)
数据库管理软件
- phpMyAdmin(网页数据库管理)
- Navicat for MySql(windows软件数据库管理)
此处使用phpEnv软件中的phpMyAdmin,登录账号是root,密码是mysql的密码
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-82cEcPLn-1685785920778)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530200621982.png)]](https://i-blog.csdnimg.cn/blog_migrate/6b0240fcfd465cdc6330129b97f285e5.png)
链接数据库
config/database.php
<?php
return [
// 默认使用的数据库连接配置
'default' => env('database.driver', 'mysql'),
// 自定义时间查询规则
'time_query_rule' => [],
// 自动写入时间戳字段
// true为自动识别类型 false关闭
// 字符串则明确指定时间字段类型 支持 int timestamp datetime date
'auto_timestamp' => true,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 时间字段配置 配置格式:create_time,update_time
'datetime_field' => '',
// 数据库连接配置信息
'connections' => [
'mysql' => [
// 数据库类型
'type' => env('database.type', 'mysql'),
// 服务器地址
'hostname' => env('database.hostname', '127.0.0.1'),
// 数据库名
'database' => env('database.database', 'mall'),
// 用户名
'username' => env('database.username', 'root'),
// 密码
'password' => env('database.password', '12345678'),
// 端口
'hostport' => env('database.hostport', '3306'),
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => env('database.charset', 'utf8'),
// 数据库表前缀
'prefix' => env('database.prefix', ''),
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 是否需要断线重连
'break_reconnect' => false,
// 监听SQL
'trigger_sql' => env('app_debug', true),
// 开启字段缓存
'fields_cache' => false,
],
// 更多的数据库配置信息
],
];
原生SQL语句
原生查询
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
// 原生查询,query只能进行查询,不能进行别的操作如插入(可以插入,但是看不到结果)
$query=Db::query("SELECT * from receiver");
dump($query);//显示结果
}
}
此时的receiver表,如下:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rqpc5GLx-1685785920778)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530202556902.png)]](https://i-blog.csdnimg.cn/blog_migrate/5aec85f73b47936a88d3f30dc90dd843.png)
输出结果如下:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Uhx2CWnJ-1685785920778)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530202702660.png)]](https://i-blog.csdnimg.cn/blog_migrate/cbab557cd29e81866f4e7b6dfc4db377.png)
原生插入和更新
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
// 原生执行,可以使用execute进行插入和更新
$execute=Db::execute("INSERT INTO `receiver` (`id`, `user_id`, `name`, `phone`, `address`, `is_default`) VALUES ('2', '2', '李四', '119119', '医院门诊', '1')");
dump($execute);//显示插入受影响的记录
$update=Db::execute("UPDATE `receiver` set `is_default`='0' where `id`='2' ");
dump($update);//显示更新受影响的记录
$query=Db::query("SELECT * from receiver");
dump($query);
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BNtVuaJT-1685785920778)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230530204031876.png)]](https://i-blog.csdnimg.cn/blog_migrate/6331309e583e24e469530e0d804551ff.png)
框架操作
查询操作
单条记录查询find
若查询结果不存在返回null,否则返回结果数组(原生查询会返回对象,需要通过转换才能转为数组)
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$find = Db::table('receiver')->find(1);//table(表名字),find(主键)
dump($find);
}
}
结果:![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JicjX1Il-1685785920779)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601183204463.png)]](https://i-blog.csdnimg.cn/blog_migrate/364067a91c492dbdc2f6a673a637390c.png)
多条数据查询 select
select方法查询结果是一个二维数组,如果结果不存在,返回空数组
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$select=Db::table('receiver')->select();//查询所有数据
dump($select);
}
}
结果![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vzzAfJzZ-1685785920779)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601183538213.png)]](https://i-blog.csdnimg.cn/blog_migrate/77b99bfc5b995edb8a7dcade5ed361ca.png)
查询某个字段的值value
value方法查询结果不存在,返回null
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$value=Db::table('receiver')->value('name');//查询字段为name的记录,默认是查询第一条记录
print_r($value);
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2u29NJJA-1685785920779)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601183714810.png)]](https://i-blog.csdnimg.cn/blog_migrate/d25256ce64bcf98ee97f38fe1753d749.png)
查询某一列的值column
column方法查询结果不存在,返回空数组
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$column=Db::table('receiver')->column('name');//查询字段为name的所有记录
print_r($column);
$column2=Db::table('receiver')->column('name','id');//查询字段为name和id的所有记录,id与name一一对应
print_r($column2);
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irtQPjdu-1685785920779)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601184108226.png)]](https://i-blog.csdnimg.cn/blog_migrate/2007c8a9867a83707a863b4f84298c60.png)
添加
添加一条数据insert
insert方法添加数据成功返回添加成功的条数,通常情况返回1
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$data=['user_id'=>'1','name'=>'洛丽塔','phone'=>'13420020232','address'=>'广东省华南师范大学','is_default'=>'1'];
$insert=Db::table('receiver')->insert($data);
print_r($insert);
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wWSR8pu1-1685785920780)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601184545185.png)]](https://i-blog.csdnimg.cn/blog_migrate/4829be0b9e543d56d6818ea9160d110d.png)
此时的数据库为
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kgWby1zr-1685785920780)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601184608379.png)]](https://i-blog.csdnimg.cn/blog_migrate/3e46c422bfc891765593db5c5cf43d6c.png)
若需要知道添加完后的id,可以使用下面的insertGetId方法(即先插入后查询)
insertGetId方法添加数据成功返回添加数据的自增主键
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$data=['user_id'=>'2','name'=>'lenck','phone'=>'13267629111','address'=>'广东省华南师范大学','is_default'=>'1'];
$insert=Db::table('receiver')->insertGetId($data);
print_r($insert);
}
}

添加多条数据insertAll
insertAll方法添加数据成功返回添加成功的条数
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$data=[
['user_id'=>'2','name'=>'lenck','phone'=>'13267629111','address'=>'广东省东软学院','is_default'=>'0'],
['user_id'=>'4','name'=>'黄宇辰','phone'=>'13267629112','address'=>'广东省华南师范大学','is_default'=>'1'],
['user_id'=>'6','name'=>'徐炯','phone'=>'13267629151','address'=>'广东省华南理工','is_default'=>'1']
];//键名需要与数据表的字段名一致,顺序可以不一致,可以为空的字段可以不填,不能为空的一定要填
$insert=Db::table('receiver')->insertAll($data);
print_r($insert);
}
}
插入了3条记录
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J9wGfVGk-1685785920780)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601185325020.png)]](https://i-blog.csdnimg.cn/blog_migrate/67c5f2f062d409bad8833c53db79f1ac.png)
修改
修改数据update
update方法返回影响数据的条数,没修改任何数据返回0
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$data=['address'=>'广西庄'];
$insert=Db::table('receiver')->where('id',1)->update($data);//此处需要用where指定,不然会把所有记录都修改
print_r($insert);
}
}
修改了1条记录
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3mRJBLpC-1685785920781)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601185811706.png)]](https://i-blog.csdnimg.cn/blog_migrate/c86c2933a4aa9fdd971bb3fd72b818a3.png)
自增inc
inc方法自增一个字段的值
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$inc=Db::table('receiver')->where('id',2)->inc('phone')->update();
print_r($inc);//phone字段值自增1
$inc=Db::table('receiver')->where('id',2)->inc('phone',5)->update();
print_r($inc);//phone字段值自增5
}
}
自减dec
dec方法自减一个字段的值
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$dec=Db::table('receiver')->where('id',2)->dec('phone')->update();
print_r($dec);//phone字段值自增1
$dec=Db::table('receiver')->where('id',2)->dec('phone',5)->update();
print_r($dec);//phone字段值自增5
}
}
删除
删除数据delete
delete方法返回影响数据的条数,没有删除返回0
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
use think\facade\view;//需引入
use think\facade\db;//引入 执行数据库原生查询
class Index extends BaseController
{
public function index()
{
$delete=Db::table('receiver')->where('name','张三')->delete();//删除name字段为张三的记录
print_r($delete);
$delete=Db::table('receiver')->delete(2);//删除主键为2的记录
print_r($delete);
$delete=Db::table('receiver')->delete(true);//删除表receriver中的所有数据
print_r($delete);
}
}
软删除useSoftDelete
并未真正删除,只是改状态
业务数据不建议真实删除数据,TP系统提供了软删除机制
$delete = Db::table('receiver')->useSoftDelete('id',3)->delete();
print_r($delete);
其他操作
save方法统一写入数据,自动判断是新增还是更新数据(以写入数据中是否存在主键数据为依据)
// 添加数据
$data=['user_id'=>'1','name'=>'洛丽塔','phone'=>'13420020232','address'=>'广东省华南师范大学','is_default'=>'1'];
$save=Db::table('receiver')->save($data);
print_r($save);
// 修改数据
$data=['id'=>3,'phone'=>'123123123'];
$save=Db::table('receiver')->save($data);
print_r($save);
ThinkPHP6模型
- 请确保你已经在数据库配置文件中配置了数据库连接信息
- 模型会自动对应数据表,模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名,并且首字母大写
- 模型自动对应的数据表名称都是遵循小写+下划线规范,如果你的表名有大写的情况,必须通过设置模型的table属性。
一、创建模型
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UHsAfXZy-1685785920781)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601192114863.png)]](https://i-blog.csdnimg.cn/blog_migrate/1659cb00a82493ebe5b92809e4809608.png)
此处应该是模型名字对应数据表名字,而不是数据库
我使用的数据表是receiver,所以我创建一个Receiver的模型
二、模型操作
在模型中可以进行数据库操作(上述讲的数据库操作都可以)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KV8aObDX-1685785920781)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601192614540.png)]](https://i-blog.csdnimg.cn/blog_migrate/3822ac68f6ca915479f6c486b4a97a7c.png)
1、find查询数据
find获取单条数据,返回的是当前模型的对象实例
app/model/Receiver.php
<?php
namespace app\model;
use think\Model;
class Receiver extends Model
{
public function find(){
$find=Receiver::where('id',2)->find();
return $find;
}
}
2、controller怎么调用model
find(主键id)查询,只使用数据表主键为id的使用主键,非id会查询失败
app/controller/Test.php
<?php
namespace app\controller;
use app\model\Receiver;//引用模型类
class Test{
public function index(){
$db=new Receiver();
$index=$db->find();
print_r($index);
}
}
在url中输入:tp.tp/index.php/test/index
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZzIjDGt0-1685785920781)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601193249958.png)]](https://i-blog.csdnimg.cn/blog_migrate/83bf3ce3c60e1e9db7ffbe3f4d2f6144.png)
3、select查询数据
select获取多条数据,返回的是当前模型的对象实例
app/model/Receiver.php
<?php
namespace app\model;
use think\Model;
class Receiver extends Model
{
public function find(){
$find=Receiver::where('id','>',2)->select();
$find=$find->toArray();//转数组返回
return $find;
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XKwQyGcO-1685785920781)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601193538449.png)]](https://i-blog.csdnimg.cn/blog_migrate/64b398b717734a50b43b704751484e85.png)
4、数据转换toArray()
5、新增数据
create静态方法添加数据,返回的是当前模型的对象实例
app/model/Receiver.php
<?php
namespace app\model;
use think\Model;
class Receiver extends Model
{
public function create(){
$create=Receiver::create([
'user_id'=>8,
'name'=>'李白',
'phone'=>'12313123123',
'address'=>'广东省囧雪',
'is_default'=>'1'
]);
//echo $create->id;//直接获取自增id
return $create;
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-74WvbC4s-1685785920782)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601194247942.png)]](https://i-blog.csdnimg.cn/blog_migrate/9dbeeb1b7be24700206a1118c514866e.png)
6、修改数据
update静态方法修改数据,返回的是当前模型的对象实例
save在取出数据后,更改字段更新数据。这种方式是最佳的更新方式
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RyeliYTR-1685785920782)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601194417548.png)]](https://i-blog.csdnimg.cn/blog_migrate/5e50dd9d3bb4462b623440ec17ca5fe6.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4igBcLH6-1685785920782)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601194535159.png)]](https://i-blog.csdnimg.cn/blog_migrate/ac640bb751719bc2bfb630be905d19ca.png)
id是需要修改的记录,改成第一个数组那样
7、删除数据
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RK2kJtid-1685785920782)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601194624038.png)]](https://i-blog.csdnimg.cn/blog_migrate/dd4e6762bc4821d0723aa4a7558d5cdb.png)
三、模型设置
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0L4qF6J4-1685785920783)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601194940093.png)]](https://i-blog.csdnimg.cn/blog_migrate/1c551bee31c10b47627c74f52938c0a7.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XZvatP10-1685785920783)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601195051463.png)]](https://i-blog.csdnimg.cn/blog_migrate/ee5ce9e5428f1c50f688d8d3a0f5bf10.png)
1、name和table
当你的数据表没有前缀的时候,name和table属性的定义是没有区别的,定义任何一个即可
app/model/Receiver.php
<?php
namespace app\model;
use think\Model;
class Receiver extends Model
{
protected $name='Receiver';
//name和table属性任选其一即可,都是选择表的意思,name属性值需要首字母大写,因为它实际上是指代模型名称
// protected $table='receiver';
public function test(){
$select=Receiver::where('id',1)->select();
return $select -> toArray();
}
}
name和table结果都是一样的
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-waVnwS23-1685785920783)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601195623208.png)]](https://i-blog.csdnimg.cn/blog_migrate/f006133d1ccf18b795542414f913684d.png)
2、pk改变主键名称
model默认的主键是id,若主键不是id,则需要pk进行改变主键名称
<?php
namespace app\model;
use think\Model;
class Receiver extends Model
{
protected $name='Receiver';
//name和table属性任选其一即可,都是选择表的意思,name属性值需要首字母大写,因为它实际上是指代模型名称
// protected $table='receiver';
protected $pk='uid';
public function test(){
$select=Receiver::where('id',1)->select();
return $select -> toArray();
}
}
3、schema设置模型对应数据表字段及类型
- 默认会自动获取(包括字段类型),但自动获取会导致增加一次查询
- schema属性一旦定义,就必须定义完整的数据表字段类型
- 类型根据php数据类型定义,如果是json类型直接定义为json即可
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lTMszBsT-1685785920783)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601195909263.png)]](https://i-blog.csdnimg.cn/blog_migrate/fb4f82ff635a763590a31de1ed443519.png)
4、disuse数据表废弃字段(数组)
若有某字段不想要,则可以使用disuse废弃
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0HiRjW5G-1685785920784)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601200008630.png)]](https://i-blog.csdnimg.cn/blog_migrate/79dbaa26233220c069efd4aef2e3e710.png)
四、模型的主要功能
1、获取器
- 获取器的作用是对模型实例的(原始)数据做出自动处理
- 命名规则:get+字段名+Attr
- 字段名是数据表字段的驼峰转换
比如字段性别中,1表示男性,0表示女性,则可以通过模型进行转换
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VmMLJzGd-1685785920784)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601200306925.png)]](https://i-blog.csdnimg.cn/blog_migrate/da930db65c82a577365f6d6a1c2edf9a.png)
此处数据表的字段是status,所以获取器的名字是getStatusAttr,若数据表的字段有下划线如user_id,则获取器的名字是getUserIdAttr

例如将时间戳改成日期
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pYrDEcJn-1685785920784)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601202925177.png)]](https://i-blog.csdnimg.cn/blog_migrate/295622fcbd11e883a4cf1c5a1c83db92.png)
2、修改器
- 修改器的主要作用是对模型设置的数据对象值进行处理
- 命名规则:set+字段名+Attr
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ocHig7WA-1685785920784)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601202936436.png)]](https://i-blog.csdnimg.cn/blog_migrate/f8d3939aeb973de016825322a2b7e202.png)
3、搜索器
- 搜索器的作用是用于封装字段(或者搜索标识)的查询条件表达式
- 命名规则:search+字段名+Attr
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pk8CIWCC-1685785920785)(C:\Users\Administrator\Desktop\ThinkPHP6\image-20230601203251534.png)]](https://i-blog.csdnimg.cn/blog_migrate/7e2329487e65ad965759f4018732910a.png)
搜索器只能在withSearch才能实现
4、检查数据
- 如果要判断数据集是否为空,不能直接使用判断
- 必须使用数据集对象的isEmpty方法判断

该文详细介绍了ThinkPHP6的安装过程,包括使用Composer下载和设置镜像源,展示了目录结构和运行步骤。接着讲解了MVC模式、视图安装与模板渲染,包括变量赋值和模板函数。还涵盖了模板引擎中的循环标签、判断标签及其用法。最后,文章深入讨论了数据库的链接、原生SQL操作以及框架内的查询、添加、修改和删除数据操作。
939

被折叠的 条评论
为什么被折叠?



