输入banner图图片脚本导航/分类

thinkphp 批量更新数据字段

  1. /*

  2.      *  @param $saveWhere :想要更新主键ID数组

  3.      *  @param $saveData    :想要更新的ID数组所对应的数据

  4.      *  @param $tableName  : 想要更新的表明

  5.      *  @param $saveWhere  : 返回更新成功后的主键ID数组

  6.      * */

  7.     public function saveAll($saveWhere,&$saveData,$tableName){

  8.         if($saveWhere==null||$tableName==null)

  9.             return false;

  10.         //获取更新的主键id名称

  11.         $key = array_keys($saveWhere)[0];

  12.         //获取更新列表的长度

  13.         $len = count($saveWhere[$key]);

  14.         $flag=true;

  15.         $model = isset($model)?$model:M($tableName);

  16.         //开启事务处理机制

  17.         $model->startTrans();

  18.         //记录更新失败ID

  19.         $error=[];

  20.         for($i=0;$i<$len;$i++){

  21.             //预处理sql语句

  22.             $isRight=$model->where($key.'='.$saveWhere[$key][$i])->save($saveData[$i]);

  23.             if($isRight==0){

  24.                 //将更新失败的记录下来

  25.                 $error[]=$i;

  26.                 $flag=false;

  27.             }

  28.             //$flag=$flag&&$isRight;

  29.         }

  30.         if($flag ){

  31.             //如果都成立就提交

  32.             $model->commit();

  33.             return $saveWhere;

  34.         }elseif(count($error)>0&count($error)<$len){

  35.             //先将原先的预处理进行回滚

  36.             $model->rollback();

  37.             for($i=0;$i<count($error);$i++){

  38.                 //删除更新失败的ID和Data

  39.                 unset($saveWhere[$key][$error[$i]]);

  40.                 unset($saveData[$error[$i]]);

  41.             }

  42.             //重新将数组下标进行排序

  43.             $saveWhere[$key]=array_merge($saveWhere[$key]);

  44.             $saveData=array_merge($saveData);

  45.             //进行第二次递归更新

  46.             $this->saveAll($saveWhere,$saveData,$tableName);

  47.             return $saveWhere;

  48.         }

  49.         else{

  50.             //如果都更新就回滚

  51.             $model->rollback();

  52.             return false;

  53.         }

  54.     }

在测试方法中调用

  1. public function test(){

  2.         //要更新的数据表的主键数组

  3.         $where['ID']=array(70,73,74,80,83);

  4.        //ID主键数组对应的待更新数据

  5.         $save=array(

  6.           array('School'=>'DK Univisity01','isExport'=>0),

  7.           array('School'=>'DK Univisity02','isExport'=>0),

  8.           array('School'=>'DK Univisity03','isExport'=>0),

  9.           array('School'=>'DK Univisity04','isExport'=>0),

  10.           array('School'=>'','isExport'=>0),

  11.          // array('School'=>'  Univisity05','isExport'=>0),

  12.         );

  13.         $f=$this->saveAll($where,$save,'want');

  14.         if(count($f['ID'])>0){

  15.            //返回更新成功的ID数组

  16.             echo "This is success :</br>";

  17.             dump($f);

  18.             echo 'ok';

  19.         }else{

  20.            //更新失败操作

  21.             echo "This is failed :</br>";

  22.             dump($f);

  23.             echo 'error';

  24.         }

  25.     }