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

Add Threads into DB Directly

Discussion in 'XenForo Development Discussions' started by NickH, May 30, 2016.

  1. NickH

    NickH Member

    I am looking to create a script that will automatically generate threads based on another API. I just wondered the best way to create a thread? Is there any documentation for creating the thread / first post?
     
  2. rainmotorsports

    rainmotorsports Well-Known Member

    Use the thread datawriter. Can grab you an example in a minute.
     
  3. NickH

    NickH Member

    Thanks, I have got as far as getting a list of threads using my script, so I just need to insert the thread which I am trying to find out how!

    Do I insert the post and then the thread? as I can see the thread needs a post_id ?
     
  4. rainmotorsports

    rainmotorsports Well-Known Member

    This is basically the data writer call from someones addon.

    Code:
    $threadDw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
          $threadDw->set('user_id', $user['user_id']);
          $threadDw->set('username', $user['username']);
          $threadDw->set('title', $title);
          $postDw = $threadDw->getFirstMessageDw();
          $postDw->set('message', $message);
          $threadDw->set('node_id', $forumId);
          $threadDw->preSave();
          $threadDw->save();
          return $threadDw->getMergedData();
    The important thing here is to set the fact this is an automated post. If you don't it will record the random IP of someone visiting the site at the time of the cron or event triggering the thread being posted. I need to get you the setting for that because I don't see it on this example.

    The above is 100% of what you need other than that. No SQL calls at all. Just insert your variables and go.
     
    NickH likes this.
  5. NickH

    NickH Member

  6. Snog

    Snog Well-Known Member

    Don't forget to add the automated flag to the postDw before the set('message'...) if the post is automated.
    Code:
    $postDw->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_IS_AUTOMATED, true);
    
     
    NickH likes this.
  7. rainmotorsports

    rainmotorsports Well-Known Member

    Thanks Snog I was trying to find that for him lol. When he said he cracked it im like not so fast lol.
     
    NickH and Snog like this.
  8. NickH

    NickH Member

    Thankyou, added that in too!
     
  9. NickH

    NickH Member

    Next question, am I best to use a server cron or try and figure out xen crons?
     
  10. rainmotorsports

    rainmotorsports Well-Known Member

    You can if you call up the autoloader, look for an example of running scripts ot crons outside xenforo. As far as the board crons just put the board into debug mode and add a cron pointing to a script contained within xenforo. The class name is the folder structure within Xenforo. I use Brand_Product_Filetype so mine would be RainDD_Whatever_Cron and then the next box is the function name.

    Up to you.
     
    Snog likes this.
  11. rainmotorsports

    rainmotorsports Well-Known Member

    Adding to what i said Just start the xenforo cron script like:
    Code:
    <?php
    /**
    * Created with PhpStorm.
    * Date: 11/5/2014
    * Product:
    */
    
    class RainDD_Product_Cron
    {
        public static function ProcessThreads()
        {
        }
    }
    The function name for the cron callback entry in this example is ProcessThreads
     
  12. Snog

    Snog Well-Known Member

    A good way to decide is if the cron MUST run at an exact time, use the server cron. If it's not critical that the cron runs at an exact time, use the xen cron.
     
  13. NickH

    NickH Member

    Thanks, I will just go down the server cron route :)

    With things like auto tweeting and other processes that occur after adding, is there a way to ensure that post thread processes happen too?
     

Share This Page