Deleting last comment does not update LastCommentUserID, DateLastComment

edited September 2010 in Vanilla 2.0 - 2.8
I noticed that when removing a comment from a discussion and it is the most recent one the LastCommentUserID and DateLastComment fields in the Discussion table were not being properly updated. I fixed this as below and added a statement to set it back to look like a new discussion if the removed comment was the only comment.

Original from Delete() on line 494 in class.commentmodel.php:
// Check to see if this is the last comment in the discussion $Data = $this->SQL ->Select('d.DiscussionID, d.LastCommentID, c.InsertUserID') ->From('Discussion d') ->Join('Comment c', 'd.DiscussionID = c.DiscussionID') ->Where('c.CommentID', $CommentID) ->Get() ->FirstRow(); if ($Data) { // If this is the last comment, get the one before and update the LastCommentID field if ($Data->LastCommentID == $CommentID) { $OldData = $this->SQL ->Select('c.CommentID') ->From('Comment c') ->Where('c.DiscussionID', $Data->DiscussionID) ->OrderBy('c.DateInserted', 'desc') ->Limit(1, 1) ->Get() ->FirstRow(); if (is_object($OldData)) { $this->SQL->Update('Discussion') ->Set('LastCommentID', $OldData->CommentID) ->Where('DiscussionID', $Data->DiscussionID) ->Put(); } }

My edits:
// Check to see if this is the last comment in the discussion $Data = $this->SQL ->Select('d.DiscussionID, d.LastCommentID, d.DateUpdated, c.InsertUserID') ->From('Discussion d') ->Join('Comment c', 'd.DiscussionID = c.DiscussionID') ->Where('c.CommentID', $CommentID) ->Get() ->FirstRow(); if ($Data) { // If this is the last comment, get the one before and update the LastCommentID field if ($Data->LastCommentID == $CommentID) { $OldData = $this->SQL ->Select('c.CommentID, c.InsertUserID, c.DateInserted') ->From('Comment c') ->Where('c.DiscussionID', $Data->DiscussionID) ->OrderBy('c.DateInserted', 'desc') ->Limit(1, 1) ->Get() ->FirstRow(); if (is_object($OldData)) { $this->SQL->Update('Discussion') ->Set('LastCommentID', $OldData->CommentID) ->Set('LastCommentUserID', $OldData->InsertUserID) ->Set('DateLastComment', $OldData->DateInserted) ->Where('DiscussionID', $Data->DiscussionID) ->Put(); } else { $this->SQL->Update('Discussion') ->Set('LastCommentID', NULL) ->Set('LastCommentUserID', NULL) ->Set('DateLastComment', $Data->DateUpdated) ->Where('DiscussionID', $Data->DiscussionID) ->Put(); } }
Sign In or Register to comment.