

新闻资讯
技术学院ThinkPHP模型CRUD必须继承think\Model并置于app\model\目录下,类名与表名对应(可配置$table和$pk),查询返回Model实例或Collection,增删改须带where条件,事件验证仅在Model操作中触发。
ThinkPHP 的模型 CRUD 不是直接写 SQL,而是通过模型类封装的链式方法操作数据库,核心在于理解 Db 类和 Model 类的区别——用 Model 才算真正“TP 模型 CURD”,它自带字段验证、事件、自动完成等能力;纯 Db::table() 属于查询构建器,不算模型层。
必须继承 think\Model,类名与数据表名默认对应(如 User 模型对应 user 表),且需放在 app\model\ 目录下。不规范命名或路径错误会导致 Class 'app\model\User' not found。
app\model\User.php
User,首字母大写,不能写成 user 或 Users
user,要在模型里显式指定:protected $table = 'tb_user';
id 时,要设置:protected $pk = 'user_id';
模型查询返回的是 think\Model 实例(单条)或 think\Collection(多条),不是原始数组。直接 echo $user->name 可以,但 echo $user['name'] 会报错。
find() 参数传整数是按主键查,传数组是复合条件(如 User::find(['status' => 1])),但更推荐统一用 where()->find()
select() 总是返回集合,即使只有一条记录,也不能直接取属性:$users = User::where('status', 1)->select(); echo $users[0]->name;
field() 显式声明,否则会被自动过滤:User::field('id, name, COUNT(*) as total')->group('status')->select();SoftDelete trait)默认不查已删除数据,要用 withTrashed() 或 onlyTrashed()
TP6+ 中 create() 方法被移除,save() 必须配合 data() 或直接传数组。空模型调用 save() 不会插入数据,必须先赋值。
$user = new User(); $user->data(['name' => '张三', 'email' => 'z@example.com']); $user->save();
User::create(['name' => '李四', 'email' => 'l@example.com']);(注意:该方法会触发验证和事件)
saveAll(),参数是二维数组:User::saveAll([
['name' => 'A', 'email' => 'a@x'],
['name' => 'B', 'email' => 'b@x']
]);$autoW
riteTimestamp = true,create_time 和 update_time 字段会自动写入,无需手动传delete() 和 save() 都依赖前置的 where() 条件,漏写等于清空或更新整张表。TP 不做“无 where 保护”,线上环境务必加日志或调试开关校验 SQL。
User::where('id', 123)->delete(); // 删除单条User::where('id', 123)->save(['status' => 2]);inc() / dec():User::where('id', 123)->inc('score', 10)->update();delete(),而是 delete(true) 或直接调用 trash()(如果模型用了 SoftDelete)最易被忽略的是模型事件和验证的触发时机:只有通过 Model 实例的 save()、create()、update() 才会触发 before_insert 等事件;用 Db::name('user')->where(...)->update(...) 完全绕过模型层,字段自动处理、事件、验证全部失效。