Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

#632 - More efficient way of updating the post count.

  • Loading branch information...
commit 5f63ac3ae6d707f8e7bf8b30b6234e1e1437abe1 1 parent 7c039f0
Jan Dorsman authored March 30, 2012

Showing 1 changed file with 1 addition and 5 deletions. Show diff stats Hide diff stats

  1. 6  post.php
6  post.php
@@ -191,12 +191,8 @@
191 191
 				$new_pid = $db->insert_id();
192 192
 			}
193 193
 
194  
-			// Count number of replies in the topic
195  
-			$result = $db->query('SELECT COUNT(id) FROM '.$db->prefix.'posts WHERE topic_id='.$tid) or error('Unable to fetch post count for topic', __FILE__, __LINE__, $db->error());
196  
-			$num_replies = $db->result($result, 0) - 1;
197  
-
198 194
 			// Update topic
199  
-			$db->query('UPDATE '.$db->prefix.'topics SET num_replies='.$num_replies.', last_post='.$now.', last_post_id='.$new_pid.', last_poster=\''.$db->escape($username).'\' WHERE id='.$tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
  195
+			$db->query('UPDATE '.$db->prefix.'topics SET num_replies=num_replies+1, last_post='.$now.', last_post_id='.$new_pid.', last_poster=\''.$db->escape($username).'\' WHERE id='.$tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
200 196
 
201 197
 			update_search_index('post', $new_pid, $message);
202 198
 

2 notes on commit 5f63ac3

Franz Liedke
Owner

I really do wonder why they didn't do it this way originally. Maybe to have an extra layer of synchronization, as the database schema is not properly normalized? But then, really, what could happen that we introduce inconsistencies in the database, eh?

Neal Poole
Collaborator

The change was made when developing 1.2:

http://punbb.informer.com/forums/post/20715/#p20715

Rewrote topic reply counting code to be less error prone. The reply count for a topic is now re-calculated whenever a post is added or removed.

But making the UPDATE query relative to the current value (as this change does) is actually the correct way to do things, assuming every other script similarly keeps data in sync.

Please sign in to comment.
Something went wrong with that request. Please try again.