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

_preSave() and _postSave() explained?

#1
Could someone please, help me understand those two methods?
PHP:
    /**
    * Post-save handling.
    */
    protected function _postSave()
PHP:
    /**
    * Pre-save handling.
    */
    protected function _preSave()
Those are not very clear to me.

In my project, I am extending XenForo_DataWriter_User and my understanding is that I need to use those two methods to save the user data to the database.

Is there example code that I missed?
 

Liam W

Well-known member
#2
_preSave is called before saving. You should set any new fields in that method, before calling parent.

_postSave is called after saving. Perform any post save actions etc.

Liam
 

Jake B.

Well-known member
#3
_preSave runs before the data is actually saved, and _postSave is run after the data is saved. _preSave could be used to validate things, _postSave could be used to alter something in another table if necessary after the data has been saved (such as updating the last post date on a thread when a new post is made)
 
#6
I am still not understanding how I am supposed to same my data

I created a new page: /index.php?account/email-preferences with a checkbox that I would like to save the state of in the DB.
PHP:
class SilverpopIntegration_XenForo_DataWriter_User extends XFCP_SilverpopIntegration_XenForo_DataWriter_User {}
HTML:
<form method="post" class="xenForm formOverlay NoFixedOverlay AutoValidator"
    action="{xen:link 'account/email-preferences-save'}"
    data-fieldValidatorUrl="{xen:link 'account/validate-field.json'}"
    data-redirect="yes">
I must be missing something...
 

Xon

Well-known member
#7
There is then: _postSaveAfterTransaction() which occurs after the transaction finishes. You should use this for stuff like Alerts and other notifications.