加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

thinkphp5使用模型save()更新数据

(2023-09-17 18:32:14)

如果你想使用TP5里边的save() 方法进行更新数据,如果按照一般情况使用where()判断更新条件的话就会出现以下报错:

类似以下代码:

r e s = m o d e l ( ′ C e s h i ′ ) − > w h e r e ( [ ′ u i d ′ = > res = model('Ceshi')->where(['uid'=>res=model(Ceshi)>where([uid=>uid])->allowField(true)->isUpdate(true)->save([‘status’=>1]);
1
正确的解决方式:
如果使用模型更新操作的时候不要使用where()来进行判断,需要使用isUpdate() 来判断,第一个参数是true,第二个参数写更新的条件。

r e s = m o d e l ( ′ C e s h i ′ ) − > a l l o w F i e l d ( t r u e ) − > i s U p d a t e ( t r u e , [ ′ u i d ′ = > res = model('Ceshi')->allowField(true)->isUpdate(true,['uid'=>res=model(Ceshi)>allowField(true)>isUpdate(true,[uid=>uid])->save([‘status’=>1]);
1
或者是在save()的第二个参数加上更新条件

r e s = m o d e l ( ′ C e s h i ′ ) − > a l l o w F i e l d ( t r u e ) − > i s U p d a t e ( t r u e ) − > s a v e ( [ ′ s t a t u s ′ = > 1 ] , [ ′ u i d ′ = > res = model('Ceshi')->allowField(true)->isUpdate(true)->save(['status'=>1],['uid'=>res=model(Ceshi)>allowField(true)>isUpdate(true)>save([status=>1],[uid=>uid]);
1
或者你用的是id主键字段为更新条件的话可以直接放在一个数组里边:

$res = model(‘Ceshi’)->allowField(true)->isUpdate(true)->save([‘status’=>1,‘id’=>1]);

https://blog.csdn.net/RSFeegg/article/details/107090308

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有