![](http://blog.mryxh.cn/wp-content/uploads/2022/11/97a752a6a339acfcca75c597fc5da908i4-300x200.jpg)
thinPHP5模型更新数据的方法有两个一个是update,一个是save方法,下面看实际案例代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\User;
class Index extends Controller{
public function index(){
$res =User::update([
'id' =>1,
'name' => 'lei'
]);
$res =User::update([
'id' =>1,
'name' => 'lei'
],[ 'id' =>2]);
$res =User::update([
'name' => 'lei'
], function ( $query ){
$query ->where( "id" , "lt" , "3" );
});
$res =User::where( "id" , "<" ,6)
->update([
'name' => 'lei'
]);
$userModel =User::get(1);
$userModel ->name= '1234' ;
$res = $userModel ->save();
$userModel = new User;
$res = $userModel ->save([
'name' => 'lei'
],[ 'id' =>1]);
$userModel = new User;
$res = $userModel ->save([
'name' => 'lei'
], function ( $query ){
$query ->where( "id" , "<" , "5" );
});
$userModel = new User;
$res = $userModel ->saveAll([
[ 'id' =>1, 'name' = 'lei1' ],
[ 'id' =>2, 'name' = 'lei2' ]
]);
dump( $res );
}
}
|
测试