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

_preSave() and _postSave() explained?

Discussion in 'XenForo Development Discussions' started by Radi, Aug 5, 2015.

  1. Radi

    Radi Member

    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?
     
  2. Liam W

    Liam W Well-Known Member

    _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
     
  3. Jake B.

    Jake B. Well-Known Member

    _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)
     
  4. Liam W

    Liam W Well-Known Member

    Just remember that postsave is called before the transaction is committed (if my memory serves me right).

    Liam
     
  5. Radi

    Radi Member

    Thank you for the clarification!
     
  6. Radi

    Radi Member

    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...
     
  7. Xon

    Xon Well-Known Member

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

Share This Page