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

XenForo_DataWriter_DiscussionMessage_Post and draft

#1
When listening to XenForo_DataWriter_DiscussionMessage_Post, what is the right way to tell if it is saving a draft? I am seeing multiple XenForo_DataWriter_DiscussionMessage_Post events for a post, presumably it relates to draft?

The intention is to monitor the save() method and do additional stuff related to the post, but only if it is the final save that commits the post.

Any help and suggestion would be much appreciated. Thanks.
 

Chris D

XenForo developer
Staff member
#2
For any DataWriter the syntax to use is:

PHP:
$this->isInsert();
Use that in the _preSave() or _postSave() functions.

It only returns true if a new record is being inserted into the table.
 

xf_phantom

Well-known member
#3
what is the right way to tell if it is saving a draft?
There's AFAIK no way to tell this.


Drafts aren't handled by the datawriters (check XenForo_Model_Draft ;) )

PHP:
public function saveDraft($key, $message, array $extraData = array(), array $viewingUser = null, $lastUpdate = null)
   {
     $this->standardizeViewingUserReference($viewingUser);

     $message = trim($message);
     if (!$viewingUser['user_id'] || !strlen($message))
     {
       return false;
     }

     if (!$lastUpdate)
     {
       $lastUpdate = XenForo_Application::$time;
     }

     $this->_getDb()->query("
       INSERT INTO xf_draft
         (draft_key, user_id, last_update, message, extra_data)
       VALUES
         (?, ?, ?, ?, ?)
       ON DUPLICATE KEY UPDATE
         last_update = VALUES(last_update),
         message = VALUES(message),
         extra_data = VALUES(extra_data)
     ", array($key, $viewingUser['user_id'], $lastUpdate, $message, serialize($extraData)));

     return true;
   }
 
#4
OK, it appears that the additional XenForo_DataWriter_DiscussionMessage_Post is coming from editing an existing post.....

I am trying to extract data from a post, and store them in a separate table, indexed by the post_id. I guess I will need to delete existing reference in my separate table, or do an update instead of insert (though this may be a bit involved).

A related question is referential integrity, is this currently supported? i.e. deleting a post will delete associated data rows - note that these are in my separate table and not the standard set of tables.