save
方法更新数据,只会更新变化的数据,对于没有变化的数据是不会进行重新更新的。为了避免这种情况,也为了更清楚的查看是否更新了,测试时统一使用下面的方法:
$user->force()->save();
1、按照官网教程写,不能更新任何数据
$user = User::find(1);
$user->name = 'thinkphp';
$user->email = 'thinkphp@qq.com';
$user->froce()->save();
2、增加了id字段,但只更新id的值,其他字段不更新。
$user = User::find(1);
$user->name = 'thinkphp';
$user->email = 'thinkphp@qq.com';
$user->id =2;
$user->force()->save();
3、 发现官网教程有这样一句:默认情况下会过滤非数据表字段的数据。
好吧,那就用allowField() 指定要写入的字段试试。
$user = UserModel::where('id','9195')->find();
$user->user_no ='Lihei1111';
$user->mobile ='1203311';
$user->name ='2211';
$user->allowField(['user_no','mobile','name'])->force()->save();
4、查看前台调试sql。成功。更新了'user_no','mobile','name' 这三个字段。
这是测试过程中发现的问题,以及自己的解决方法。
疑问:难道每次都要指定要写入字段吗?