$DataWriter->set('node_id', $newnode) to move a thread

ForestForTrees

Well-known member
Hello,

Is it good practice to use the datawriter on 'node_id' code to move a thread from one subforum to another? Or is there a higher-level way to do it that might be more modular?

Example code: (source: tutorial by Fuhrmann)
Code:
$threads = $threadModel->getThreads($conditionals, $fetchOptions);
foreach ($threads as $thread)
{
 $dw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
 $dw->setExistingData($thread);
 $dw->set('node_id', $cronOptions['toForum']);
 $dw->save();
}
 
I wouldn't just change the node_id because it's not going to perform other necessary tasks for you.

This is the code block I use to move threads, the Inline Mod model has a nice function there for you to use.

PHP:
if ($this->isChanged('forum_id'))
{
    $threadModel = XenForo_Model::create('XenForo_Model_Thread');
    $threads     = $threadModel->getThreads(
        array(
            'node_id'         => $this->getExisting('forum_id'),
            'discussion_type' => 'xr_item_discussion'
        )
    );

    $inlineModModel                = $this->getModelFromCache('XenForo_Model_InlineMod_Thread');
    $inlineModModel->enableLogging = false;

    $inlineModModel->moveThreads(
        array_keys($threads),
        $this->get('forum_id'), array(
            'skipPermissions'  => true,
            'approveModerated' => true,
            'checkSameForum'   => false
        )
    );
}

That was my full use of it, this is the portion you'll be interested in:
PHP:
$inlineModModel->moveThreads(
    $arrayOfThreadIds,
    $newForumId, 
    array(
        'skipPermissions'  => true,
        'approveModerated' => false,
        'checkSameForum'   => true
    )
);
You'll need to replace the first argument with an array of the thread IDs you want to move, and replace the second argument with the new forum ID.

Cheers,

Jeff
 
Thanks so much, Jeff. That was exactly my concern.

It's going to take me a little while to understand your code well enough to integrate it into my addon, as I don't know the XF classes at all (literally at all). For example, I don't know XenForo_Model, getModelFromCache (for example, what object is it a method of?), and I don't even know how to look up the documentation about objects or methods at this point!

In the meantime, off the top of your head, can you think about any permanent danger from using the above code in production in the short run? I'm not worried about checking permissions or anything like that. I've tested the datawriter code in my first post already and it seems to work wonderfully, so I put it into production yesterday before I read your post.
 
After looking over the code involved, I cannot see any potential problems with the way you are doing it (if you are not concerned about the permissions of who's moving them). But in general it is just wise to use pre-existing methods that are meant to do what you need to do. It reduces code-redundancy, and it allows your script to continue functioning should XF update the way threads are moved.
 
Thanks for taking the time, Jeff. I will upgrade it when I have time to study your example, but until then, I'll use this with much greater confidence.
 
Top Bottom