Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -1039,32 +1039,30 @@ |
1040 | 1040 | } |
1041 | 1041 | |
1042 | 1042 | /** |
1043 | | - * INSERT ... ON DUPE UPDATE wrapper, inserts an array into a table, optionally updating if |
1044 | | - * duplicate primary key found |
| 1043 | + * INSERT ... ON DUPLICATE KEY UPDATE wrapper, inserts an array into a |
| 1044 | + * table, optionally updating if duplicate primary key found |
1045 | 1045 | * |
1046 | | - * $a may be a single associative array, or an array of these with numeric keys, for |
1047 | | - * multi-row insert. |
| 1046 | + * $rows may be a single associative array, or an array of these with |
| 1047 | + * numeric keys, for multi-row insert. |
1048 | 1048 | * |
1049 | | - * Usually aborts on failure |
1050 | | - * If errors are explicitly ignored, returns success |
| 1049 | + * Usually aborts on failure. If errors are explicitly ignored, returns success. |
1051 | 1050 | * |
1052 | 1051 | * @param $table String: table name (prefix auto-added) |
1053 | | - * @param $a Array: Array of rows to insert |
| 1052 | + * @param $rows Array: Array of rows to insert |
1054 | 1053 | * @param $fname String: Calling function name (use __METHOD__) for logs/profiling |
1055 | 1054 | * @param $onDupeUpdate Array: Associative array of fields to update on duplicate |
1056 | 1055 | * |
1057 | 1056 | * @return bool |
1058 | 1057 | */ |
1059 | | - function insertOrUpdate( $table, $a, $fname = 'DatabaseBase::insertOrUpdate', $onDupeUpdate = array() ) { |
1060 | | - |
1061 | | - if ( isset( $a[0] ) && is_array( $a[0] ) ) { |
1062 | | - $keys = array_keys( $a[0] ); |
| 1058 | + function insertOrUpdate( $table, $rows, $fname = 'DatabaseBase::insertOrUpdate', $onDupeUpdate = array() ) { |
| 1059 | + if ( isset( $rows[0] ) && is_array( $rows[0] ) ) { |
| 1060 | + $keys = array_keys( $rows[0] ); |
1063 | 1061 | } else { |
1064 | | - $keys = array_keys( $a ); |
| 1062 | + $keys = array_keys( $rows ); |
1065 | 1063 | } |
1066 | 1064 | |
1067 | | - //Get what is only to be set if inserted |
1068 | | - $where = array_diff( $a, $onDupeUpdate ); |
| 1065 | + // Get what is only to be set if inserted |
| 1066 | + $where = array_diff( $rows, $onDupeUpdate ); |
1069 | 1067 | |
1070 | 1068 | $res = $this->select( |
1071 | 1069 | $table, |
— | — | @@ -1074,12 +1072,13 @@ |
1075 | 1073 | ); |
1076 | 1074 | |
1077 | 1075 | if ( $res ) { |
1078 | | - // Where there is a different value to set if this is being "updated", use the $onDupeUpdate value for that to |
1079 | | - // replace the original option (if it was an insert), and replace the column name with the value read from |
1080 | | - // the existing row |
1081 | | - foreach( $where as $k => $v ) { |
1082 | | - if ( isset( $onDupeUpdate[$k] ) ) { |
1083 | | - $options[$k] = str_replace( $k, $res[0]->{$k}, $onDupeUpdate[$k] ); |
| 1076 | + // Where there is a different value to set if this is being |
| 1077 | + // "updated", use the $onDupeUpdate value for that to replace the |
| 1078 | + // original option (if it was an insert), and replace the column |
| 1079 | + // name with the value read from the existing row |
| 1080 | + foreach ( $where as $key => $unused ) { |
| 1081 | + if ( isset( $onDupeUpdate[$key] ) ) { |
| 1082 | + $options[$key] = str_replace( $key, $res[0]->{$key}, $onDupeUpdate[$key] ); |
1084 | 1083 | } |
1085 | 1084 | } |
1086 | 1085 | } else { |