php setfield,setField更新字段

setField 更新字段值

1、功能:更新一个或多个字段官方手册:只提供一个更新一个字段的方法

查看源码发现,其实将参数换成数组,可以同时更新多个字段

2、源码:/thinkphp/library/think/db/Query.php中的 setField方法

aade9ce47f139e00bfd6ac799929c591.png

3、参数与返回值序号输入参数返回值12个参数时('字段名','字段值')

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  setField( [ 'salary' => [ 'exp', 'salary + 500' ] ] );可能有同学对: [ 'salary' => [ 'exp', 'salary + 500' ] ] ) 写法不能理解,随着后面对查询表达式的学习,就理解了,这里仅仅知道可以同时更新多条记录即可。

5、实例演示:先查看一下当前tp5_staff表中数据

b208b66cd85941d59ca5ede2bf8592db.png

任务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 的记录已更新

6274f0df57af434940eb96ccd05de107.png

任务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"

}此时,查看数据表:

80ee889c887cce22bcf0b4c01767aa84.png

任务3 :将tp_staff表中年龄在20到30岁之间员工,工资加1000,入职时间统一修改为:2012-12-12注意:该操作涉及多记录更新,与前面实例不同更新前表中数据:

17ade9d45c61a411b938078a9dc3fffd.pngIndex.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"

}

}此时,表中数据如下:

a671951b41691012087064a79953b5ce.png

6、总结1.update方法与setField方法都可以完成同样的工作;

2.日常开发中,推荐使用update方法;

3.当仅更新单条记录中某个字段值时,用setField方法更简洁和直观。

7、作业setField方法的若干用法,建议每个都上机操作一遍~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值