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

Get thread ID when using the thread datawriter?

Discussion in 'XenForo Development Discussions' started by Jarvis, Jun 19, 2016.

  1. Jarvis

    Jarvis New Member

    Is there a way for me to get the thread ID of the thread created using a datawriter?

    For example, I want to automatically generate a post for a thread when I create it.

    PHP:
    // Create thread datawriter
    // Create thread

    // Create post 
    // in thread where ID = thread created
     
  2. Thomas.B

    Thomas.B Well-Known Member

    The answer is in the source code :)
    PHP:
       protected function _discussionPostSave()
       {
         
    $threadId $this->get('thread_id');
         ...
       }
    Btw, don't forget to check if it's actually a new thread!
     
  3. Jarvis

    Jarvis New Member

    Thank you. Apologies!
     
  4. Jarvis

    Jarvis New Member

    Here is my function to create the new thread:

    PHP:
    public function actionCreateThread($forumID$title$message)
    {
        
    $poster $this->getRRDBot();
        
    $writer XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
        
    $writer->set('user_id'$poster['userID']);
        
    $writer->set('username'$poster['username']);
        
    $writer->set('title'$title);
        
    $postWriter $writer->getFirstMessageDw();
        
    $postWriter->set('message'$message);
        
    $writer->set('node_id'$forumID);
        
    $writer->preSave();
        
    $writer->save();
        return 
    $writer->getMergedData();
    }
    How do I incorporate the function you sent me?

    I noticed:

    PHP:
    /**
    * Gets the current value of the discussion ID for this discussion.
    *
    * @return integer
    */
    public function getDiscussionId()
    {
       return 
    $this->get($this->getDiscussionKeyName());
    }
    In the XF discussion datawriter, but not sure how to utilize it.
     
  5. Jarvis

    Jarvis New Member

    I got it:

    PHP:
    public function actionCreateThread($forumID$title$message)
    {
        
    $poster $this->getRRDBot();
        
    $writer XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
        
    $writer->set('user_id'$poster['userID']);
        
    $writer->set('username'$poster['username']);
        
    $writer->set('title'$title);
        
    $postWriter $writer->getFirstMessageDw();
        
    $postWriter->set('message'$message);
        
    $writer->set('node_id'$forumID);
        
    $writer->preSave();
        
    $writer->save();
        return 
    $writer->getDiscussionId();
    }
     
  6. Xon

    Xon Well-Known Member

    getMergedData will return an array which contains a 'thread_id' attribute.
     
    Jarvis likes this.

Share This Page