zoukankan      html  css  js  c++  java
  • Thinkphp5.0 的使用模型Model更新数据

    Thinkphp5.0 的使用模型Model更新数据


    (1)使用update()方法进行更新数据


    一、where条件写在更新数据中

    (这种情况更新的数据,必须含主键)

            $res = User::update([
                'id' => 2,
                'email' => '121@qq.com'
            ]);
            //返回修改之后model的整个对象信息
            dump($res);

    二、where条件使用update()的第二个参数,传递数组

            $res = User::update([
                'email' => '123@qq.com'
            ],['id'=>2]);
            //返回修改之后model的整个对象信息
            dump($res);

    三、where条件使用update()的第二个参数,传递闭包函数

           $res = User::update([
                'email' => '555@qq.com'
            ],function($query){
                $query->where(['id'=>2]);
            });
            //返回修改之后model的整个对象信息
            dump($res);

    四、使用where条件

            $res = User::where('id','=',2)->update([
                'email'=>'666@qq.com'
            ]);
            //返回影响的行数
            dump($res);

    (2)使用save()方法

    方式一:

            $model = User::get(2);
            $model->email = '777@qq.com';
            $res = $model->save();
            //返回影响的行数
            dump($res);

    方式二:

            $model = new User();
            $res2 = $model->save([
                'email' => '999@qq.com'
            ],['id'=>2]);
            //返回影响的行数
            dump($res2);

    方式三:

            $model = new User();
            $res = $model->save([
                'email' => '000@qq.com'
            ],function($query){
                $query->where(['id'=>2]);
            });
            //返回影响的行数
            dump($res);

    使用saveAll()方法更新多个数据:

            $model = new User();
            $res = $model->saveAll([
                ['id' => 2,'email' => '122@qq.com'],
                ['id' => 3,'email' => '123@qq.com'],
                ['id' => 4,'email' => '124@qq.com']
            ]);
            //返回数组
            dump($res);

  • 相关阅读:
    bzoj 3262: 陌上花开
    hdu 5618 Jam's problem again
    bzoj 1176: [Balkan2007]Mokia
    bzoj 2683: 简单题
    Codevs 1080 线段树练习(CDQ分治)
    bzoj 3223: Tyvj 1729 文艺平衡树
    bzoj 1503: [NOI2004]郁闷的出纳员
    bzoj 1208: [HNOI2004]宠物收养所
    bzoj 1588: [HNOI2002]营业额统计
    bzoj 3224: Tyvj 1728 普通平衡树
  • 原文地址:https://www.cnblogs.com/gyfluck/p/9430654.html
Copyright © 2011-2022 走看看