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

Datawriter or Update-Query?


I have a question ragarding to the update of database fields. How far I can see there are multiple possibilities to update a row. Here are two examples:

$dwPost = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
$dwPost->set('post_date', XenForo_Application::$time);
$db->update('xf_post', array('post_date' => XenForo_Application::$time), 'post_id = '.$db->quote($post['post_id']));

What is the better solution for an update? ... and why is this the better solution? When I have to use the first example and when the second one?


Well-known member
It comes down to what you're changing. If it's something like post_date you could use the second method with no issue (at a glance I didn't catch something in the data writer about that, though the post date may confuse readers if it isn't chronological). As an example of using the first, if editing the post content outside of normal means like the through the editor, you should be using the first method so that the search index could be updated and that the message is validated before saving.

Look over the data writer methods like XenForo_DataWriter_DiscussionMessage::_postSave() as they indicate when certain actions are triggered based on whether or not something has been changed, deleted, or inserted. They give more or less a clear picture of what is necessary when it comes to whether or not you use the data writer method or just a simple update query. Being familiar with XenForo's structure and data relations helps significantly in figuring out what's safe to change outside of the data writers, so a bit of reading through the code would be beneficial. :)

In the end, there's no harm in using the data writers if you're in doubt.

thanks for your replay it was very helpful. I'm answering so late because the DataWriter of XenForo is a very complex thing. But if you know how to use it's a very powerful tool. I have overwritten XenForo_DataWriter_Discussion::updateCountersAfterMessageSave to get the result I want. It's a better solution then update every table manually (xf_post, xf_thread, xf_forum). Now I can change the 'post_date' of xf_post and the parent tables (xf_thread, xf_forum) will be updated automatically if necessary. Also the 'position' column gets an update after the DataWriter has been saved, thanks to XenForo_Model_Like::recalculatePostPositionsInThread :)