侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

thinkphp中的模型操作

ThinkPHP  /  管理员 发布于 8年前   486

新建模型

有两个方法:

一个是手动创建

1、新建model文件夹

2、新建文件user.php。最好名字和表名对应

3、写代码

<?phpnamespace app\index\model;use think\Model;class User extends Model{//如果表名和文件名不是对应的,用下面代码修改protected $table = 'think_user';    }

第二个是用命令:

>php think make:model index/Blog

模型实例化

有三种方式:

1、用静态方法

use app\index\model\User; $li= User::get(1);

2、用加载类

use think\Loader;$user= Loader::model('user'); $li= $user::get(1);

3、用系统方法

$user= model('User'); $li= $user::get(1);

数据库的查询操作

get的意思是,查询主键为1的数据。后面是查询name=thinkphp的数据

//取出主键为1的数据$user = User::get(1);// 使用数组查询$user = User::get(['name' => 'thinkphp']);

也可以使用系统查询方法

$user = new User();$user->where('name', 'thinkphp')->find();

查询多条数据

用all()方法

// 根据主键获取多个数据$list = User::all('1,2,3');// 或者使用数组$list = User::all([1,2,3]);foreach($list as $key=>$user){    echo $user->name;}// 使用数组查询$list = User::all(['status'=>1]);// 使用闭包查询$list = User::all(function($query){    $query->where('status', 1)->limit(3)->order('id', 'asc');});foreach($list as $key=>$user){    echo $user->name;}

或者用查询方法查看

$user = new User();// 查询数据集$user->where('name', 'thinkphp')    ->limit(10)    ->order('id', 'desc')    ->select();

查询某个字段值

// 获取某个用户的积分User::where('id',10)->value('score');// 获取某个列的所有值User::where('status',1)->column('name');// 以id为索引User::where('status',1)->column('name','id');User::where('status',1)->column('id,name');

也可以用动态查询

// 根据name字段查询用户$user = User::getByName('thinkphp');// 根据email字段查询用户$user = User::getByEmail('[email protected]');

数据新增

用save方法

$user           = new User;$user->name     = 'thinkphp';$user->email    = '[email protected]';$user->save();

用data批量方法保存

$user = new User;$user->data([    'name'  =>  'thinkphp',    'email' =>  '[email protected]']);$user->save();

最常用的是实例化的时候导入post或者get数据,然后保存保存##我是感觉这里比较重要

//如果需要过滤非数据表字段的数据,可以使用:$user = new User($_POST);// 过滤post数组中的非数据表字段数据$user->allowField(true)->save();//如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:$user = new User($_POST);// post数组中只有name和email字段会写入$user->allowField(['name','email'])->save();

获取插入数据id

$user           = new User;$user->name     = 'thinkphp';$user->email    = '[email protected]';$user->save();// 获取自增IDecho $user->user_id;

插入多条数据

$user = new User;$list = [    ['name'=>'thinkphp','email'=>'[email protected]'],    ['name'=>'onethink','email'=>'[email protected]']];$user->saveAll($list);

也可以使用助手函数

// 使用model助手函数实例化User模型$user = model('User');// 模型对象赋值$user->data([    'name'  =>  'thinkphp',    'email' =>  '[email protected]']);$user->save();或者进行批量新增:$user = model('User');// 批量新增$list = [    ['name'=>'thinkphp','email'=>'[email protected]'],    ['name'=>'onethink','email'=>'[email protected]']];$user->saveAll($list);

数据删除,需要先查询,再删除

删除一条

$user = User::get(1);$user->delete();

删除多条

User::destroy(1);// 支持批量删除多个数据User::destroy('1,2,3');// 或者User::destroy([1,2,3]);

根据条件删除

// 删除状态为0的数据User::destroy(['status' => 0]);还支持使用闭包删除,例如:User::destroy(function($query){    $query->where('id','>',10);});或者通过数据库类的查询条件删除User::where('id','>',10)->delete();

数据更新,需要先查询,再更新

$user = User::get(1);$user->name     = 'thinkphp';$user->email    = '[email protected]';$user->save();

根据条件更新

$user = new User;// save方法第二个参数为更新条件$user->save([    'name'  => 'thinkphp',    'email' => '[email protected]'],['id' => 1]);

post提交直接更新

$user = new User();// 过滤post数组中的非数据表字段数据$user->allowField(true)->save($_POST,['id' => 1]);//如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:$user = new User();// post数组中只有name和email字段会写入$user->allowField(['name','email'])->save($_POST, ['id' => 1]);

强制更新,防止添加

$user = new User;$list = [    ['id'=>1, 'name'=>'thinkphp', 'email'=>'[email protected]'],    ['id'=>2, 'name'=>'onethink', 'email'=>'[email protected]']];$user->isUpdate()->saveAll($list);

用数据库类添加

$user = new User;$user->where('id', 1)    ->update(['name' => 'thinkphp']);或者使用:$user = new User;$user->update(['id' => 1, 'name' => 'thinkphp']);

推荐教程:《TP5》

以上就是thinkphp中的模型操作的详细内容,更多请关注其它相关文章!


  • 上一条:
    ThinkPHP6 事件与多应用
    下一条:
    thinkphp6中使用twig
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客