setField 更新字段
最编程
2024-04-02 09:21:10
...
-
setField 更新字段值
- 1、功能:更新一个或多个字段
- 2、源码:/thinkphp/library/think/db/Query.php中的 setField方法
- 3、参数与返回值
-
4、语法(单条更新与多条更新):
- 一、更新单条记录(必须是主键)
- 二、同时更新多条记录 (必须是条件表达式)
-
5、实例演示:
- 任务1:将id=1007的员工的工资salary 更新为8567 ,完成单字段更新
- 任务2:将id=1011的员工的年龄、工资,入职日期修改,完成多字段更新
- 任务3 :将tp_staff表中年龄在20到30岁之间员工,工资加1000,入职时间统一修改为:2012-12-12
- 6、总结
- 7、作业
-
setField 更新字段值
setField 更新字段值
1、功能:更新一个或多个字段
官方手册:只提供一个更新一个字段的方法
查看源码发现,其实将参数换成数组,可以同时更新多个字段
2、源码:/thinkphp/library/think/db/Query.php中的 setField方法
3、参数与返回值
序号 | 输入参数 | 返回值 |
---|---|---|
1 | 2个参数时('字段名','字段值') 1个参数时(与字段名对应的数组) |
受影响记录条数 即更新数量 |
4、语法(单条更新与多条更新):
一、更新单条记录(必须是主键)
以单条记录为例,下面给出常用的四种用法:
- 更新一个字段,前面必须设置更新条件,如where,参数必须是二个字符串:
Db::table( 完整表名) -> where(更新条件) -> setField('字段名' , '字段值'); // 例如: Db::table('tp5_staff') -> where('id',1024) -> setField('salary' , 8500);
- 多字段更新,数据放在一个数组中传入(主键在数组中):
// 1.创建员工信息数组 $data = []; $data[id] = 主键值; //设置更新条件 $data[ 字段1 ] = 字段值1 ; $data[ 字段2 ] = 字段值2 ; ...... //执行更新操作 Db::table( 表名 ) -> setField($data);
- 多字段更新,数据放在一个数组中(主键不在数组中),方法前必须给出更新条件:
// 1.创建员工信息数组 $data = []; $data[ 字段1 ] = 字段值1 ; $data[ 字段2 ] = 字段值2 ; ...... //执行更新操作 Db::table( 表名 ) -> where( 更新条件 ) -> setField($data);
- 多字段更新时,直接将主键,更新数组全部做为参数,一次性传入:
//将更新条件与数据一次性传入 Db::table( 表名 )->setField(['主键字段'=> 主键值,'字段1'=>字段值1,'字段2'=>字段值2,...]); //如果更新主键不放在参数数组中,则在方法前添加where方法 Db::table( 表名 )->where( 更新条件 ) -> setField(['字段1'=>字段值1,'字段2'=>字段值2,...]);
二、同时更新多条记录 (必须是条件表达式)
手册上,并没有给出同时更新多条记录的方法,其实更新多条记录也很简单,只要在更新方法前,设置好更新条件即可。
- 基本语法:
//如果更新主键不放在参数数组中,则在方法前添加where方法 Db::table( 表名 )->where( 更新条件 ) -> setField(['字段1'=>字段值1,'字段2'=>字段值2,...]); //例如,将表中员工的工资小于3000元的,加薪500元,福利好吧? Db::table('tp5_staff' )->where( 'salary < 3000' ) -> setField( [ 'salary' => [ 'exp', 'salary + 500' ] ] );
可能有同学对: [ 'salary' => [ 'exp', 'salary + 500' ] ] ) 写法不能理解,随着后面对查询表达式的学习,就理解了,这里仅仅知道可以同时更新多条记录即可。
5、实例演示:
先查看一下当前tp5_staff表中数据
任务1:将id=1007的员工的工资salary 更新为8567 ,完成单字段更新
- Index.php 控制器代码如下:
<?php namespace app\index\controller; use think\Db; class Index { public function index(){ // 1.更新数据 Db::table('tp5_staff') -> where('id',1007) -> setField('salary',8567); // 2.查看更新结果 dump(Db::table('tp5_staff')->find(1007)); } }
- 运行结果:
array(7) { ["id"] => int(1007) ["name"] => string(9) "潘金莲" ["sex"] => int(0) ["age"] => int(39) ["salary"] => float(8567) ["dept"] => int(3) ["hiredate"] => string(10) "2016-03-20" }
- 表中id = 1007 的记录已更新
任务2:将id=1011的员工的年龄、工资,入职日期修改,完成多字段更新
- 我们先查看一下李团长现在的情况:
<?php namespace app\index\controller; use think\Db; class Index { public function index(){ dump(Db::table('tp5_staff')->find(1011)); } }
- 更新前:李云龙信息如下:
array(7) { ["id"] => int(1011) ["name"] => string(9) "李云龙" ["sex"] => int(1) ["age"] => int(39) ["salary"] => float(4800) ["dept"] => int(1) ["hiredate"] => string(10) "2011-09-12" }
- 现在我们修改一下Index.php:
<?php namespace app\index\controller; use think\Db; class Index { public function index(){ //1.创建要更新的数据 $data = []; $data['id'] = 1011; $data['age'] = 49; $data['salary'] = 9850; $data['hiredate'] = '2005-10-20'; // 2.更新数据 Db::table('tp5_staff') -> setField($data); // 3.查看更新结果 dump(Db::table('tp5_staff')->find(1011)); } }
- 再次运行,查看结果:
array(7) { ["id"] => int(1011) ["name"] => string(9) "李云龙" ["sex"] => int(1) ["age"] => int(49) ["salary"] => float(9850) ["dept"] => int(1) ["hiredate"] => string(10) "2005-10-20" }
- 此时,查看数据表:
任务3 :将tp_staff表中年龄在20到30岁之间员工,工资加1000,入职时间统一修改为:2012-12-12
注意:该操作涉及多记录更新,与前面实例不同
- 更新前表中数据:
- Index.php 控制器代码如下:
<?php namespace app\index\controller; use think\Db; class Index { public function index(){ //1.创建要更新条件 $map['age'] = ['between',[20,30]]; //2.创建更新数据 $data = []; $data['dept'] = 3; $data['salary'] = ['exp','salary + 1000']; $data['hiredate'] = '2012-12-12'; // 2.更新数据 Db::table('tp5_staff') -> where($map) -> setField($data); // 3.查看更新结果 dump(Db::table('tp5_staff')->where($map)->order('age')->select()); } }
- 运行结果(4条记录受影响):
array(4) { [0] => array(7) { ["id"] => int(1001) ["name"] => string(6) "郭靖" ["sex"] => int(0) ["age"] => int(22) ["salary"] => float(6679) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } [1] => array(7) { ["id"] => int(1025) ["name"] => string(9) "小铃铛" ["sex"] => int(0) ["age"] => int(22) ["salary"] => float(6739) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } [2] => array(7) { ["id"] => int(1020) ["name"] => string(6) "虚竹" ["sex"] => int(0) ["age"] => int(28) ["salary"] => float(5765) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } [3] => array(7) { ["id"] => int(1002) ["name"] => string(9) "洪七公" ["sex"] => int(0) ["age"] => int(29) ["salary"] => float(5365) ["dept"] => int(3) ["hiredate"] => string(10) "2012-12-12" } }
- 此时,表中数据如下:
6、总结
1.update方法与setField方法都可以完成同样的工作;
2.日常开发中,推荐使用update方法;
3.当仅更新单条记录中某个字段值时,用setField方法更简洁和直观。
7、作业
setField方法的若干用法,建议每个都上机操作一遍~~
推荐阅读
-
Windows 一直在配置更新 50% 所需的时间 - Nuggets
-
AD域建设管理(二)|python3+ldap3管理AD域实践(批量创建OU、用户、修改密码、更新OU和用户)
-
电费单多久更新一次?电费多久更新一次?
-
我的 "生活缴费 "电费账单余额多久更新一次?我的电费账单多久更新一次?
-
电费余额多久更新一次
-
使用 kubekey 更新 K8S 证书
-
2023年更新,鲜果如何在快手好物联盟(快配)运营?快手快捷配送的操作流程是
-
C 语言信息收集(持续更新)
-
切换如何更新氛围,并进入 hekate 界面
-
任天堂 Switch 注册账号教程--Switch 的游戏是不锁区的,也就是说美国买的游戏可以用日本账号打开玩,游戏内容是一样的,没有区别。(有些游戏比较特殊,比如《Splatoon 2》,日版只有日文版,美版只有英文版)。 正如我之前所说,日版游戏和美版游戏虽然不同,但就我所见,大部分游戏都是可以买到的,只是可能发售时间不同,语言补丁更新的快慢不同,价格也略有不同。 最后,填写表格并勾选同意使用条款,然后确定以获取 5 位数的验证码。 获取注册码 在刚才看到的 Switch 屏幕上输入五位数的启动代码。 帐户登录完成 欢迎光临。 请将本文加入书签,并随时转发给您的新朋友!