Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Looking for a simple way to fetch a discussion

edited August 2010 in Vanilla 2.0 - 2.8
I'm setting up vanilla for a small forum, and things are going well but there is one thing missing: I'd like to show the most recent discussion from a specific forum as a newspost on the frontpage of the site,

My problem is that I can't seem to find an easy-to-use API to fetch discussions/comments. I've been through the Discussioncontroller, but that code doesn't seem very re-usable, and the json-api app is kinda daunting since I've never used json before. :(

What would be the easiest way to do this in?


  • Options
    What about just connecting Vanilla's db and selecting:
    SELECT *, `GDN_Discussion`.`DiscussionID` AS ID FROM `GDN_Discussion` ORDER BY `GDN_Discussion`.`DateUpdated` DESC LIMIT 0, 1;
    to retreive a last updated discussion and selecting:
    SELECT * FROM `GDN_Comment` WHERE `GDN_Comment`.`DiscussionID` = ID ORDER BY `GDN_Comment`.`DateInserted` ASC;
    to retreive comments for the discussion, where ID is DiscussionID returned in the first select.
  • Options
    @klip Wouldn't that be DESC to grab the last comment if we're sorting by DateInserted (datetime)?

    Also, you might want to use some kind of a database abstraction layer behind your SQL command to protect yourself against injection. If you're using any kind of CMS, you should use its built in methods for accessing the database.
  • Options
    My plan was to keep it as integrated with Vanilla and use the CustomPages plugin for this..

    On a side not, I've now gotten progress with simply copying the discussionscontroller+views and making a "blogcontroller", but it's all very hackish and unmaintainable, I need a better solution.. :/

    I'd wish there was someone who could explain exactly where and how the discussioncontroller does its queries, so I could reuse them for a CustomPages page and still be able to get all the userinfo (avatars etc.) and formatting.

    I'm very thankful for the help so far, though :)
  • Options
    @Sergio965 It depend's...

    You need the last discussion updated, so you use DateUpdated DESC.

    Then you need all comments for the discussion from the first to last inserted, so DateInserted ASC. But if you want to have last on top, then you use DESC.

    @chi1 I see.

    Good luck with that. Would be nice to have a blog possibility. Anyway, it might be better to write Blog as new Garden Application, though I don't understand the architecture of Vanilla yet.
  • Options
    TimTim Operations Vanilla Staff
    Are you writing this in a plugin?

    Vanilla Forums COO [GitHub, Twitter,]

  • Options
    TimTim Operations Vanilla Staff
    edited August 2010
    In general, @chi1, Vanilla's controllers do not directly execute queries. We make requests to "Models" which implement the queries as methods. Typically we fetch discussions using the DiscussionModel::Get() method, however I just looked at it and it seems to force order by DateLastComment, so it would be impossible for you to use that method to fetch a specific discussion.

    If you're using a plugin to implement these changes, you can create a method on DiscussionModel using our event framework, like this:

    public DiscussionModel_GetOrdered_Create($Sender) { // Code here }

    As for what the code in there should kinda look like... check out applications/vanilla/models/class.discussionmodel.php for inspiration

    Vanilla Forums COO [GitHub, Twitter,]

  • Options
    atm, I'm writing it as an application, and I'm close to completion :) I solved it by copying the DiscussionModel into a BlogModel, with an edited "->Get" called "-->GetBlog".

    I'll be sure to post it when I'm fully done.
  • Options

    It's very rough, but I figured it might be useful to someone. :)
  • Options
    MarkMark Vanilla Staff
    Nice work!
Sign In or Register to comment.