php-think_php5.1模型更新数据

实体类
<?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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修罗_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值