实体类
<?php
namespace app\index\Model;
use think\Model;
class Score extends Model {
protected $pk = 'uuid';
protected $table = 'tp_score';
/**
* @var mixed|string
*/
private $user;
}
实现类
<?php
namespace app\index\controller;
//调用模型
use app\index\Model\Score;
class Update {
public function index() {
//指定uuid更新记录1
$score = Score::where('uuid', '13579')->find();
$score->user = '老黑';
$score->save();
//指定id更新记录2
<?php
namespace app\index\controller;
use app\index\Model\Score;
class Update {
//http://localhost/tp5.test/public/index.php/index/Update
public function index() {
$score = Score::where('uuid', '13579')->find();
$score->user = '老黑';
$score->save();
$score = Score::get('13579');
$score->user = '李白';
$score->save();
//上述更新只有值不同时才会更新,假如你想强制更新
$score->force()->save();
// 指定uuid来更新数据
$score = new Score();
$score->save(
[
'user' => '李白',
'math' => 0
],
[
'uuid' => '13579'
]
);
$score = new Score();
// saveAll方法只能通过主键来更新,所有数组里第一条属性一定是主键
$score->saveAll([
[
'uuid'=>'13579',
'user'=>'李白',
'english'=>0
],
[
'uuid'=>'13813822',
'user'=>'李白',
'english'=>0
]
]);
Score::where('uuid', '13579')->update([
'user' => '黧黑',
'math' => 88
]);
// 下面这种是静态方法,放回的是对象实例
Score::update([
'uuid' => '13579',
'user' => 'll',
'math' => 121
]);
//模型的新增和修改都是save()进行执行的,他采用了自动识别体系来完成。
//实例化模型后调用save()方法表示新增,查询数据后调用save()表示修改。
//如果在save()传入更新修改条件后也表示修改。
//如果编写的代码比较复杂,可以用isUpdate()方法显示操作。
$score = new Score();
//强制更新
$score->isUpdate(true)->save();
//强制修改
$score->isUpdate(false)->save();
}
} $score = Score::get('13579');
$score->user = '李白';
$score->save();
//上述更新只有值不同时才会更新,假如你想强制更新
$score->force()->save();
// 指定uuid更新记录3
$score = new Score();
$score->save(
[
'user' => '李白',
'math' => 0
],
[
'uuid' => '13579'
]
);
$score = new Score();
// saveAll方法只能通过主键来更新,所有数组里第一条属性一定是主键
$score->saveAll([
[
'uuid'=>'13579',
'user'=>'李白',
'english'=>0
],
[
'uuid'=>'13813822',
'user'=>'李白',
'english'=>0
]
]);
//指定uuid更新记录4
Score::where('uuid', '13579')->update([
'user' => '黧黑',
'math' => 88
]);
// 下面这种是静态方法,放回的是对象实例
Score::update([
'uuid' => '13579',
'user' => 'll',
'math' => 121
]);
//!!!注意
//1.模型的新增和修改都是save()进行执行的,他采用了自动识别体系来完成。
//2.实例化模型后调用save()方法表示新增,查询数据后调用save()表示修改。
//3.如果在save()传入更新修改条件后也表示修改。
//4.如果编写的代码比较复杂,可以用isUpdate()方法强制指定操作。
$score = new Score();
//强制更新
$score->isUpdate(true)->save();
//强制修改
$score->isUpdate(false)->save();
}
}