From 179c45abd7ab068e5f4e0d22bfdb62621a3b6d16 Mon Sep 17 00:00:00 2001 From: zoic21 <1536036+zoic21@users.noreply.github.com> Date: Mon, 15 Jun 2026 09:44:18 +0000 Subject: [PATCH] fix(history): set correct tableName for archived rows in all() history::all() unions the history and historyArch tables and hydrates every row as a history object, so rows coming from the archive kept the default _tableName 'history'. Carry the source table as a literal column in the (non-grouped) query so PDO populates _tableName correctly, ensuring later save()/remove() target the right table. Closes #3097 --- core/class/history.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/class/history.class.php b/core/class/history.class.php index 0035469145..c159048cea 100644 --- a/core/class/history.class.php +++ b/core/class/history.class.php @@ -368,7 +368,7 @@ public static function all($_cmd_id, $_startTime = null, $_endTime = null, $_gro } $sql = ''; if ($_groupingType == null || strpos($_groupingType, '::') === false) { - $sql .= 'SELECT ' . DB::buildField(__CLASS__); + $sql .= 'SELECT ' . DB::buildField(__CLASS__) . ', a._tableName'; } else { $goupingTypeDelta = explode('||', $_groupingType); if (count($goupingTypeDelta) > 1) { @@ -414,7 +414,7 @@ public static function all($_cmd_id, $_startTime = null, $_endTime = null, $_gro } $sql .= ' FROM ('; } - $sql .= ' (SELECT * from history WHERE value is not null AND cmd_id=:cmd_id '; + $sql .= ' (SELECT *, \'history\' as _tableName from history WHERE value is not null AND cmd_id=:cmd_id '; if ($_startTime !== null) { $sql .= ' AND datetime>=:startTime'; } @@ -423,7 +423,7 @@ public static function all($_cmd_id, $_startTime = null, $_endTime = null, $_gro } $sql .= ') '; $sql .= ' UNION ALL '; - $sql .= ' (SELECT * from historyArch WHERE value is not null AND cmd_id=:cmd_id '; + $sql .= ' (SELECT *, \'historyArch\' as _tableName from historyArch WHERE value is not null AND cmd_id=:cmd_id '; if ($_startTime !== null) { $sql .= ' AND `datetime`>=:startTime'; } @@ -477,13 +477,13 @@ public static function all($_cmd_id, $_startTime = null, $_endTime = null, $_gro 'cmd_id' => $_cmd_id, 'startTime' => $_startTime ); - $sql = 'SELECT ' . DB::buildField(__CLASS__); + $sql = 'SELECT ' . DB::buildField(__CLASS__) . ', a._tableName'; $sql .= ' FROM ('; - $sql .= ' (SELECT * from history WHERE value is not null AND cmd_id=:cmd_id AND `datetime`<=:startTime'; + $sql .= ' (SELECT *, \'history\' as _tableName from history WHERE value is not null AND cmd_id=:cmd_id AND `datetime`<=:startTime'; $sql .= ' ORDER BY datetime DESC LIMIT 1'; $sql .= ') '; $sql .= ' UNION ALL '; - $sql .= ' (SELECT * from historyArch WHERE value is not null AND cmd_id=:cmd_id AND `datetime`<=:startTime'; + $sql .= ' (SELECT *, \'historyArch\' as _tableName from historyArch WHERE value is not null AND cmd_id=:cmd_id AND `datetime`<=:startTime'; $sql .= ' ORDER BY datetime DESC LIMIT 1'; $sql .= ') '; $sql .= ')a ';