Properly change username via script?

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?

Thanks!
 
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.
 
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.
Cool thanks, managed to get it working using the DataWriter.
 
@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:
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.

Cheers.
 
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:
PHP:
    public static function extendUserModel($class, array &$extend)
    {
        if (!self::$_addedUsernameChange)
        {
            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'));
        }
    }
 
Top Bottom