You are correct - I just verified that behaviour on my forum as well:
Subforum posts are only updated on the main page if the parent forum has posts or you manually put a value in the "LastPosted" column of that parent forum
LaB wrote:
I think that you were closer than I was. I finally had some time to dig into this and may have a solution.
I'm definitely NOT an SQL guru, so hopefully someone will check this over.
I'm using SQL Express 2005 and here is what I changed:
In the Database, under Programmability / Functions / Scalar-valued Functions, I altered the yaf_forum_lasttopic function.
Here is the new version:
-- if subforum has newer topic/message, make it last for parent forum
if (@TopicID is not null and @Posted is not null and (@LastPosted < @Posted OR (@LastPosted is null and @Posted is not null ) )) begin
Basically, it adds another qualifier onto the expression, just in case the parent is NULL, but the child is not.. I don't know for sure, but I think it may have had a hard time comparing a NULL value to a valid date..
That appears to have fixed it for me, but I would have an expert check it out before throwing it up in production..
Here is the full function
USE [yourforumdb]
GO
/****** Object: UserDefinedFunction [dbo].[yaf_forum_lasttopic] Script Date: 04/11/2009 14:38:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[yaf_forum_lasttopic]
(
@ForumID int,
@UserID int = null,
@LastTopicID int = null,
@LastPosted datetime = null
) RETURNS int AS
BEGIN
-- local variables for temporary values
declare @SubforumID int
declare @TopicID int
declare @Posted datetime
-- try to retrieve last direct topic posed in forums if not supplied as argument
if (@LastTopicID is null or @LastPosted is null) begin
SELECT
@LastTopicID=a.LastTopicID,
@LastPosted=a.LastPosted
FROM
[dbo].[yaf_Forum] a
JOIN [dbo].[yaf_vaccess] x ON a.ForumID=x.ForumID
WHERE
a.ForumID=@ForumID and
(
(@UserID is null and (a.Flags & 2)=0) or
(x.UserID=@UserID and ((a.Flags & 2)=0 or x.ReadAccess<>0))
)
end
-- look for newer topic/message in subforums
if exists(select 1 from [dbo].[yaf_Forum] where ParentID=@ForumID)
begin
declare c cursor for
SELECT
a.ForumID,
a.LastTopicID,
a.LastPosted
FROM
[dbo].[yaf_Forum] a
JOIN [dbo].[yaf_vaccess] x ON a.ForumID=x.ForumID
WHERE
a.ParentID=@ForumID and
(
(@UserID is null and (a.Flags & 2)=0) or
(x.UserID=@UserID and ((a.Flags & 2)=0 or x.ReadAccess<>0))
)
open c
-- cycle through subforums
fetch next from c into @SubforumID, @TopicID, @Posted
while @@FETCH_STATUS = 0
begin
-- get last topic/message info for subforum
SELECT
@TopicID = LastTopicID,
@Posted = LastPosted
FROM
[dbo].[yaf_forum_lastposted](@SubforumID, @UserID, @TopicID, @Posted)
-- if subforum has newer topic/message, make it last for parent forum
if (@TopicID is not null and @Posted is not null and (@LastPosted < @Posted OR (@LastPosted is null and @Posted is not null ) )) begin
SET @LastTopicID = @TopicID
SET @LastPosted = @Posted
end
fetch next from c into @SubforumID, @TopicID, @Posted
end
close c
deallocate c
end
-- return id of topic with last message in this forum or its subforums
RETURN @LastTopicID
END
Edited by user
15 years ago |
Reason: added entire function