db()->getTable(); } /** * 批量更新字段 * 获取DB 对象并赋予 模型本身的 connection 连接 * @return mixed */ public static function updateWhenCase($datas) { if(is_array($datas) && count($datas) > 0 ){ $ids = array_keys($datas); if(is_array(reset($datas))){ $fields = array_keys(reset($datas)); $sql= " update ". self::getDb() ." set "; foreach($fields as $field ){ $sql.= " `$field` = ( case id"; foreach($datas as $id => $data ){ $sql.= " when $id then ".self::stringFilter($data[$field])." "; } $sql.= " end ),"; } $sql = substr($sql,0,-1); $sql.= " where id in (".implode(",",$ids).")"; Db::execute($sql); } } } public static function stringFilter($string) { if(is_null($string)) { return 'null'; } return "'" . str_replace("'","\'",$string) . "'"; } }