|
|
@@ -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;
|