瀏覽代碼

fxied bug in Mysql when primary key is not ID

BILLING_OWNER primary key is 'id'
Piotr Labudda 9 年之前
父節點
當前提交
fdd7cdd310
共有 2 個文件被更改,包括 14 次插入11 次删除
  1. 13 10
      SE/se-lib/Core/Database/Mysql.php
  2. 1 1
      SE/se-lib/V.php

+ 13 - 10
SE/se-lib/Core/Database/Mysql.php

@@ -464,21 +464,22 @@ class Core_Database_Mysql extends Core_Database {
 		$structure=self::describe_table_value($table); //todo to cache optimize
 		$primary=self::show_index_value($table); //todo to cache optimize
 
-		if (!isset($sql_obj->$primary) || $sql_obj->$primary <= 0) {
+		$primaryKey = V::geti($primary, '', $sql_obj);
+		if ($primaryKey <= 0) {
+			$this->_set_error("Missing primary key '{$primary}' in table '{$table}'!");
 			return -3;
 		}
-		$id = $sql_obj->$primary;
 
-		// check id record $id exists
-		if (($curr_obj = $this->get_by_id( $table, $sql_obj->$primary )) == null) {
+		// check if record $primaryKey exists
+		if (($curr_obj = $this->get_by_id( $table, $primaryKey )) == null) {
+			$this->_set_error("Record '{$primaryKey}' not exists in table '{$table}'!");
 			return -2;
 		}
 
 		// check if enything changed
 		$changed = false;
-		$fields_to_change = get_object_vars($sql_obj);
-		foreach ($fields_to_change as $k => $v) {
-			if ($k == $primary) continue;
+		foreach (get_object_vars($sql_obj) as $k => $v) {
+			if (strtolower($k) == strtolower($primary)) continue;
 			if ($v == $curr_obj->$k) {// === ?
 				unset($sql_obj->$k);
 			} else {
@@ -518,7 +519,7 @@ class Core_Database_Mysql extends Core_Database {
 			}
 			$sql_arr [] = "`{$k}`={$v}";
 		}
-		$sql = "update `{$table}` set ".implode(",", $sql_arr)." where `ID`='{$id}' limit 1; ";
+		$sql = "update `{$table}` set ".implode(",", $sql_arr)." where `ID`='{$primaryKey}' limit 1; ";
 		$this->query($sql);
 
 		$returnError = false;
@@ -534,8 +535,10 @@ class Core_Database_Mysql extends Core_Database {
 		$affected = $this->affected_rows();
 		if ($affected || $skipDbErrorAddHist) {
 			$returnCode = 1;
-			$sql_obj->ID_USERS2 = $sql_obj->$primary;
-			unset($sql_obj->$primary);
+			$sql_obj->ID_USERS2 = $primaryKey;
+			foreach (get_object_vars($sql_obj) as $k => $v) {
+				if (strtolower($k) == strtolower($primary)) unset($sql_obj->$k);
+			}
 			$new_id = $this->ADD_NEW_OBJ("{$table}_HIST", $sql_obj);
 			if ($new_id) {
 				$returnCode += 1;

+ 1 - 1
SE/se-lib/V.php

@@ -16,7 +16,7 @@ class V {
 		foreach ((array)$from as $fieldName => $value) {
 			$lowerFrom[ strtolower($fieldName) ] = $value;
 		}
-		return V::get($name, $default, $lowerFrom, $type, $filterCallback);
+		return V::get(strtolower($name), $default, $lowerFrom, $type, $filterCallback);
 	}
 
 	/**