ThinkPHP5数据库实例详解 /setField更新字段

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 控制器代码如下:
 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的员工的年龄、工资,入职日期修改,完成多字段更新

  • 我们先查看一下李团长现在的情况:
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:
 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 控制器代码如下:
 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方法的若干用法,建议每个都上机操作一遍~~