🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ <?php namespace app\index\controller; use app\index\model\Staff; class Index { public function index(){ //创建模型对象 $model = new Staff; //创建自定义数据 $data[] = ['id' => 1010,'name'=>'宋门庆']; $data[] = ['id' => 1011,'name'=>'马金莲']; //批量更新,自动识别更新和写入 $result = $model -> saveAll($data,true); //反馈执行信息 echo $result ? '更新成功!<br />' : '更新失败~~<br />' ; //查看更新后的信息 echo '更新后的记录信息:<br />'; //遍历数据对象数组 foreach ($result as $list){ dump($list -> getData()); } } } ~~~ 批量更新 ~~~ <?php namespace app\index\controller; use app\index\model\Staff; class Index { public function index(){ $dataBefore = Staff::where('name','like','%云%')->select(); if (!empty($dataBefore)){ //查看一下更新前的数据对象中的原始数据 echo '1. 查看一下更新前的数据对象中的原始数据<br />'; foreach ($dataBefore as $before){ dump($before->getData()); } //更新数据对象中的原始数据 foreach ($dataBefore as $list){ $list -> setAttr('salary','6000'); $data[] = $list -> getData(); } //用数据对象中的原始数据更新表中对应字段信息 $dataAfter = (new Staff()) -> saveAll($data); if (!empty($dataAfter)){ echo '更新成功!<br />'; //查看更新完成后返回的数据对象 echo '2. 查看一下更新后的数据对象中的原始数据<br />'; foreach ($dataAfter as $after){ dump($after->getData()); } }else{ echo '更新失败!'; } } else { echo '没有满足条件的记录!'; } } } ~~~ 输出 ~~~ 1. 查看一下更新前的数据对象中的原始数据 array(6) { ["id"] => int(1009) ["name"] => string(9) "楚云飞" ["sex"] => int(1) ["salary"] => float(8100) ["dept"] => string(9) "开发部" ["hiredate"] => string(10) "2017-01-01" } array(6) { ["id"] => int(1010) ["name"] => string(9) "李云龙" ["sex"] => int(1) ["salary"] => float(2000) ["dept"] => string(9) "开发部" ["hiredate"] => string(10) "2017-02-01" } 更新成功! 2. 查看一下更新后的数据对象中的原始数据 array(6) { ["id"] => int(1009) ["name"] => string(9) "楚云飞" ["sex"] => int(1) ["salary"] => string(4) "6000" ["dept"] => string(9) "开发部" ["hiredate"] => string(10) "2017-01-01" } array(6) { ["id"] => int(1010) ["name"] => string(9) "李云龙" ["sex"] => int(1) ["salary"] => string(4) "6000" ["dept"] => string(9) "开发部" ["hiredate"] => string(10) "2017-02-01" } ~~~