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

Properly change username via script?

Discussion in 'XenForo Development Discussions' started by AUSFIFA, Nov 19, 2015.


    AUSFIFA Member

    I know it's possible to just update the username column in xf_user table, but doing it this way wont update any of the username references in the other tables, such as "last_post_username" in xf_thread.

    The only way to change the username properly in all database tables is to do it by editing their profile in the Xenforo ACP, but is there anyway I can achieve this via script? Is there a function which handles username changes in Xenforo that I can access?

  2. Chris D

    Chris D XenForo Developer Staff Member

    If you use the User DataWriter to update a user's username, and the "updateContentOnUsernameChange" option is enabled then, actually, it already is handled automatically.

    See XenForo_Model_User::$userContentChanges for more details.

    This can be (and should be) updated by add-ons to include their own tables and fields that should be updated.
    AUSFIFA likes this.

    AUSFIFA Member

    Cool thanks, managed to get it working using the DataWriter.
  4. Jeremy P

    Jeremy P Well-Known Member

    @Chris D I'm currently hooking into init_dependencies and appending to the $userContentChanges array directly. Is there a better way to do it?

    IIRC, this was what was advised to me back when the array was in the data writer and I just updated it to use the model when it moved over.
    Last edited: Nov 19, 2015
  5. Chris D

    Chris D XenForo Developer Staff Member

    Probably just use load_class_model with a XenForo_Model_User hint.
    Jeremy P likes this.
  6. Jeremy P

    Jeremy P Well-Known Member

    That makes more sense, I'll give it a shot.

    I remember having trouble appending to it with a custom class since it's a static array, but I suppose using that hook to append to it directly should work.

  7. Chris D

    Chris D XenForo Developer Staff Member

    Actually doing it via init_dependencies is reasonable.

    But at least tying it to the Model where it is called from means it is only appended when it is maybe going to be needed.

    FWIW this is what it looks like (shorter version) in the Gallery:
        public static function extendUserModel($class, array &$extend)
            if (!
    self::$_addedUsernameChange true;
    XenForo_Model_User::$userContentChanges['xengallery_album'] = array(array('album_user_id''album_username'));
    XenForo_Model_User::$userContentChanges['xengallery_album_watch'] = array(array('user_id'));
    Jeremy P likes this.
  8. Jeremy P

    Jeremy P Well-Known Member

    Thanks Chris, got it working great. :)

Share This Page