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

Set node_id in xf_thread

LPH

Well-known member
#1
The following works:

PHP:
$node_id = XenWord::getForumIdForPost( $post->ID );
$thread_id = $thread['thread_id'];
$update = "UPDATE `xf_thread` SET `node_id` = ? WHERE `thread_id` = {$thread_id}";
XenForo_Application::getDb()->fetchOne($update, $node_id);
But I wanted to simply use the datawriter and cannot seem to get it to work.

PHP:
$node_id= XenWord::getForumIdForPost( $post->ID );
$writer->set( 'node_id', $node_id );
I've also tried setExistingData. Am I missing an easy way to change the node_id in xf_thread?
 

Liam W

Well-known member
#2
This should work:

PHP:
$dw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$dw->setExistingData($threadId);
$dw->set('node_id', $forumId);
$dw->save();
Make sure you're setting the existing data, and calling save.

Liam
 

LPH

Well-known member
#3
Right, except that doesn't work and it's my fault for not posting all of the lines of code.

This works.
PHP:
        $writer = XenForo_DataWriter::create( 'XenForo_DataWriter_DiscussionMessage_Post' );
        $writer->setExistingData( $post_id );
        $writer->set( 'message', $body );
        $writer->preSave();

        if ( ! $writer->hasErrors() ) {

            $writer->save();
            $node_id = XenWord::getForumIdForPost( $post->ID );
            $thread_id = $thread['thread_id'];
            $update = "UPDATE `xf_thread` SET `node_id` = ? WHERE `thread_id` = {$thread_id}";
            XenForo_Application::getDb()->fetchOne($update, $node_id);

        }
This fails to change the node_id but does not register an error.
PHP:
        $node_id = XenWord::getForumIdForPost( $post->ID );

        $writer = XenForo_DataWriter::create( 'XenForo_DataWriter_DiscussionMessage_Post' );
        $writer->setExistingData( $post_id );
        $writer->set( 'node_id', $node_id );
        $writer->set( 'message', $body );
        $writer->preSave();

        if ( ! $writer->hasErrors() ) {

            $writer->save();
This fails and gives the error "An exception occurred: The field 'node_id' was not recognised"

PHP:
        $writer = XenForo_DataWriter::create( 'XenForo_DataWriter_DiscussionMessage_Post' );
        $writer->setExistingData( $post_id );
        $writer->set( 'message', $body );
        $writer->preSave();

        if ( ! $writer->hasErrors() ) {

            $writer->save();

            $node_id = XenWord::getForumIdForPost( $post->ID );
            $writer = XenForo_DataWriter::create( 'XenForo_DataWriter_DiscussionMessage_Post' );
            $writer->setExistingData( $post_id );
            $writer->set( 'node_id', $node_id );
            $writer->save();
 

truonglv

Formerly Nobita.Kun
#4
by default XenForo did not use node_id for table xf_post. If you want to so must add new column and register field into datawriter.
 

LPH

Well-known member
#6
Hmm... I'm using this in another function in the same class and it sets it. Now I'm really lost. LOL. OK. Time to step back and figure out what I'm doing because I'm wanting the xf_thread and not xf_post.

PHP:
$forum_id= XenWord::getForumIdForPost( $post_id );
$ThreadWriter->set( 'node_id', $forum_id );
 

Daniel Hood

Well-known member
#7
PHP:
$writer = XenForo_DataWriter::create( 'XenForo_DataWriter_DiscussionMessage_Post' );
$writer->setExistingData( $post_id );
$writer->set( 'message', $body );
$writer->preSave();

if ( ! $writer->hasErrors() ) {
$writer->save();
$node_id = XenWord::getForumIdForPost( $post->ID );
$thread_id = $writer->get('thread_id');

$dw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$dw->setExistingData($thread_id);
$dw->set('node_id', $node_id);
$dw->save();
}
?
 
Last edited:

LPH

Well-known member
#8
OK. This works :)

PHP:
$writer = XenForo_DataWriter::create( 'XenForo_DataWriter_DiscussionMessage_Post' );
$writer->setExistingData( $post_id );
$writer->set( 'message', $body );
$writer->preSave();

if ( ! $writer->hasErrors() ) {

	$writer->save();

	$node_id = XenWord::getForumIdForPost( $post->ID );
	$thread = $writer->getMergedData();

	$writer = XenForo_DataWriter::create( 'XenForo_DataWriter_Discussion_Thread' );
	$writer->setExistingData( $thread['thread_id'] );
	$writer->set( 'node_id', $node_id );
	$writer->save();
}