🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 5 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at the following url: | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Original Author <author@example.com> | // | Your Name <you@example.com> | // +----------------------------------------------------------------------+ // // $Id:$ /* * @param $saveWhere :想要更新主键ID数组 * @param $saveData :想要更新的ID数组所对应的数据 * @param $tableName : 想要更新的表明 * @param $saveWhere : 返回更新成功后的主键ID数组 * */ public function saveAll($saveWhere, &$saveData, $tableName) { if ($saveWhere == null || $tableName == null) return false; //获取更新的主键id名称 $key = array_keys($saveWhere) [0]; //获取更新列表的长度 $len = count($saveWhere[$key]); $flag = true; $model = isset($model) ? $model : M($tableName); //开启事务处理机制 $model->startTrans(); //记录更新失败ID $error = []; for ($i = 0; $i < $len; $i++) { //预处理sql语句 $isRight = $model->where($key . '=' . $saveWhere[$key][$i])->save($saveData[$i]); if ($isRight == 0) { //将更新失败的记录下来 $error[] = $i; $flag = false; } //$flag=$flag&&$isRight; } if ($flag) { //如果都成立就提交 $model->commit(); return $saveWhere; } elseif (count($error) > 0 & count($error) < $len) { //先将原先的预处理进行回滚 $model->rollback(); for ($i = 0; $i < count($error); $i++) { //删除更新失败的ID和Data unset($saveWhere[$key][$error[$i]]); unset($saveData[$error[$i]]); } //重新将数组下标进行排序 $saveWhere[$key] = array_merge($saveWhere[$key]); $saveData = array_merge($saveData); //进行第二次递归更新 $this->saveAll($saveWhere, $saveData, $tableName); return $saveWhere; } else { //如果都更新就回滚 $model->rollback(); return false; } } ?> ~~~