Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

saved comment's ID

edited May 2008 in Vanilla 1.0 Help
anyone knows how to get the ID of a comment just after being saved?

I assume that knowing it before is at least approximate, but I just can't achieve to get it after saving.



  • Options
    Is this for an extension?
  • Options
    yes it is, any idea that could help? thx!
  • Options
    edited May 2008
    It appears the saving is done in the CommentManager::SaveComment() method on line 289. The relevant delegate to hook in to would be "CommentManager"->"PostSaveNewComment". However, something is not right in that block of code...

    In PHP5 it should work fine (because PHP5 doesn't implicitly clone objects), but PHP4 would duplicate the $Comment object (on line 348 of the same file). Perhaps a dev could look in to why it was done this way...

    Standard Disclaimer: I could be totally wrong...
  • Options
    Good find sirlancelot--it appears that $SaveComment is intended to be a temporary copy, as it is set on line 321, presumably because validation may change some properties. It could have been used to compare the results of validation when debugging...

    It's been like this since Vanilla 0.9.2, maybe earlier.
  • Options

    It's interesting to me because a Delegate Parameter for the $SaveComment object is set, but the new comment ID (after the object is saved), is set on the $Comment object. I think what should be done (in short) is $this->DelegateParameters['Comment'] = &$SaveComment; be changed to $this->DelegateParameters['Comment'] = &$Comment;Some more thought needs to be done here though...

    Standard Disclaimer: I could be totally wrong...
  • Options
    I agree, but think we should go further and just eliminate $SaveComment altogether--the fact that it works one way in PHP 5 but differently in earlier versions makes it a headache to troubleshoot for extension developers.

    0.9.2 didn't even have delegation, so I think using $SaveComment on the DelegateParameter is purely by accident. $Comment is set back to $SaveComment further down on both sides of the IF (lines 338 and 439) so replacing every $SaveComment with $Comment and removing the three copying lines should have no effect other than making PHP 5 and older versions of PHP work the same.

    I can run this by Mark to make sure, just to be safe.
  • Options
    MarkMark Vanilla Staff
    I say change away as long as it doesn't break anything. I can't remember why I did it that way - seems strange now, but I'm sure there was some reason for it at the time...
  • Options
    The Famous Programming Words ofMark O'Sullivan...seems strange now, but I'm sure there was some reason for it at the time...
  • Options
    haha! That's what you use // Comments for :) I confess though that I even forget to document my code sometimes...
  • Options
    This is now fixed in svn
  • Options
    ok, now the function should be attached to
    • 'PostSaveNewComment' delegate of the Comment Manager, or
    • the 'PostSaveComment' of the DiscussionForm?
    because the one of the CommentManager doesn't delegate the comment, so i don't know how to access its properties (specially its ID)

    sorry for the ignorance :P
  • Options
    PostSaveNewComment will still work, even though it appears that it won't.

    The trick is that the &$Comment doesn't just return the comment object, but a pointer to that object, and the pointer is set about 30 lines above that. So your extension will look at where that pointer points, which happens to be the same comment object that just got the ID assigned.

    Try it out!
This discussion has been closed.