diff --git a/includes/engine/class-tmdo-hook-bus.php b/includes/engine/class-tmdo-hook-bus.php index 7d20802..3f6ba30 100644 --- a/includes/engine/class-tmdo-hook-bus.php +++ b/includes/engine/class-tmdo-hook-bus.php @@ -321,35 +321,18 @@ final class TMDO_Hook_Bus { self::$internal_ops[ $lock_key ] = true; try { - // 檢查列是否存在 - $exists = $wpdb->get_var( - $wpdb->prepare( - "SELECT id FROM `{$table}` WHERE `{$id_col}` = %d", - $entity_id - ) + // 單一 upsert,避免 SELECT→UPDATE/INSERT 的 read-modify-write race。 + $result = TMDO_DB::upsert( + $table, + array( + $id_col => $entity_id, + $col => $db_value, + ), + array( $col ), + $id_col, + array( '%d', $format ) ); - if ( $exists ) { - // UPDATE - $result = $wpdb->update( - $table, - array( $col => $db_value ), - array( $id_col => $entity_id ), - array( $format ), - array( '%d' ) - ); - } else { - // INSERT - $result = $wpdb->insert( - $table, - array( - $id_col => $entity_id, - $col => $db_value, - ), - array( '%d', $format ) - ); - } - // 清除快取 TMDO_Cache_Orchestrator::invalidate( $type, $entity_id, $group );