1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Thread Trigger

Discussion in 'XenForo Development Discussions' started by Ferity, May 27, 2015.

  1. Ferity

    Ferity Member


    Short introduction:
    Every time when a thread is posted in a specific node, I want a poll to be added automatically.

    I have already added a Procedure that adds the Poll with the following parameters: (ThreadID, Question, Answer 1, Answer 2, Answer 3). This Procedure has been tested and is working smoothly.

    Now to automatize this, I wanted to add an After Insert Trigger on xf_thread. But this runs into an error.

    My trigger is the following (we're using MySql if relevant):
    delimiter //
    create trigger xftables_Trig_SugPolls
    after insert
    on  xf_thread
    for each row
    if(new.node_id = 4) then
        call FSuggestionPolls(new.thread_id,'Do you think that these suggestions should be implemented?','Yes','No','I don\'t care');
    end if;
    end //
    delimiter ;

    In my procedure I edit the following tables: xf_thread, xf_poll, xf_poll_response

    Creating Threads in node != 4 works fine. But when I create a thread in node 4, I'm experiencing an error. The call FSuggestionPolls Procedure was tested on already existing threads and worked perfectly fine.

    Do I possibly have to set the trigger to another database because xf_table might be too early in the thread creation process?

    Best regards
    Last edited: May 27, 2015
  2. MtoR

    MtoR Well-Known Member


    Hum why not doing this is in PHP ?

    IMO xF class extension is a far easier way to perform this.

  3. Ferity

    Ferity Member

    Hey MtoR,

    thanks for your fast answer!

    I've only got some database knowledge and do not know how to code such in Php and how to implement it properly.
    Are there any guides you could recommend to learn it?

    Best regards
  4. MtoR

    MtoR Well-Known Member


    If you don't have PHP knowledge that might be complicated.

    You would have to extend the thread model postSave method to create your poll and execute a query.

    Perhaps there is a tutorial that explains that, or you can check a mod that does something kinda like that to get inspiration.

  5. Ferity

    Ferity Member

    Could you tell me where the postSave method can be found?
    I should be able to include the call of my method myself.
  6. MtoR

    MtoR Well-Known Member

    In library/XenForo/Model/Thread.php.

    You may have to add a condition to check that it's a new thread not an update.

  7. Ferity

    Ferity Member

    Ok, I'll take a look on it.

    Thanks very much for your help!

    Best regards

Share This Page