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

Create Post via Code

Discussion in 'XenForo Development Discussions' started by AGD-Andy, Apr 28, 2014.

  1. AGD-Andy

    AGD-Andy Member

    I have a cron job that was on my old vBulletin forum that I want to convert. The steps in the process were like this:

    * Query database for payments, ordered by date
    * if there were NEW payments:
    * Check Month - If there is not a thread for this month, create a new one. If there is, then create a new reply for that thread
    * Mark new payments as submitted in DB

    Essentially it created a new thread each month to list out payments so that everyone was always aware of payments made. Also created the thread so that users could add their own comments there.

    What I don't know how to do with xenForo is how to create threads and replies via code :) Any starter-help would be greatly appreciated!
  2. Liam W

    Liam W Well-Known Member

    Use the datawriter:

    $writer XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
    $writer->set('user_id', <userid of the poster>);
    $writer->set('username', <username of the poster>);
    $writer->set('node_id', <id of the node the thread is in>);
    $writer->set('title', <thread title>);
    $writer->set('prefix_id', <id of the thread prefix, if any>);

    $postWriter $writer->getFirstMessageDw();
    $postWriter->set('message', <thread contents>);


    //Thread is saved.
    This is a basic overview. Have a look at the library/XenForo/ControllerPublic/Forum.php file, line 518 (the actionAddThread function).
    AGD-Andy likes this.
  3. fly

    fly Member

    Can someone explain how you would add posts to an already created thread?
  4. Liam W

    Liam W Well-Known Member

    Use the post datawriter. Set the thread ID field.
  5. fly

    fly Member

    From some searches, I assume it's this: XenForo_DataWriter_DiscussionMessage_Post

    But how do I set the threadid? Sorry if this is documented somehow, I'm a recent convert from vB and haven't figured all this out yet.
  6. Liam W

    Liam W Well-Known Member

    In my above post, you see I create a data writer instance and then set things?

    The first parameter of the set method is the field name, the second the value.

    You must set message,userid,username,threadid for posts.
    fly likes this.
  7. fly

    fly Member

    Got it. Thanks!
  8. $writer->set('node_id', <id of the node the thread is in>);

    the is no 'node_id' !
  9. Liam W

    Liam W Well-Known Member

    The node id is the id of the node you want to post in. It is actually the forum ID - it's the node id because it comes from the node table.

  10. ok !
    How we can know the node_id for the up code sample ?
    Is there some variable with the curent node ?
  11. HotCopper

    HotCopper Member

    I've used a slightly modified version from above example to include tag, the thread and post have been created in db and serialized tag is in the thread, but the tag row is not updated to include the latest_tagged_content.

    What is the best way to update the tag row with new thread/post?

    $writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
    $writer->set('user_id', $user_id);
    $writer->set('username', "xxxxx");
    $writer->set('node_id', $node['node_id']);
    $writer->set('title', "Initial Post");
    $writer->set('tinhte_xentag_tags', serialize(Array('tag_text'=>$tag['tag_text'])));
    $postWriter = $writer->getFirstMessageDw();
    $postWriter->set('message', "Initial Post");
  12. HotCopper

    HotCopper Member

    I found the answer for anyone interested

    $writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
    $writer->set('user_id', $user_id);
    $writer->set('username', "NSX News");
    $writer->set('node_id', $node['node_id']);
    $writer->set('title', "Initial Post");
    $postWriter = $writer->getFirstMessageDw();
    $postWriter->set('message', "Initial Post");

Share This Page