1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

How to rebuild the custom field cache

Discussion in 'Development Tutorials [Archive]' started by Rigel Kentaurus, Dec 16, 2011.

  1. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    All the custom fields for the users are stored in a couple of places.
    1. The first one is the database table xf_user_field_value in which it maintains a relation between the field_id and the user_id
    2. The second one is a php serialized version of it, that is stored inside custom_fields in the xf_user_profile table
    If you were to code an addon that writes to a custom field, you would need to keep updating both the table and the serialized version. Using a custom field might be a good idea since there are already hooks in the system for it, and should it need to be editable there is all the functionality already coded in for making it appear in Personal Information or Preferences.

    This value is recalculated when the user edits his information and clicks on "save". Another way is when you go to the Maintenance tasks in the AdminCP and "Rebuild user information"

    In some cases, you might not want to rebuild all user information as that would also do posts, and any other information associated to the user. If you wanted to only rebuild the custom fields, a simple PHP file with the following code will suffice.

    Code:
    RebuildCustomFields.php
    
    Code:
    <?php
    
    $startTime = microtime(true);
    
    require('./library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader('./library');
    
    XenForo_Application::initialize('./library');
    XenForo_Application::set('page_start_time', $startTime);
    XenForo_Application::setDebugMode(true);
    
    $db = XenForo_Application::getDb();
    $userIds = $db->fetchCol("SELECT user_id FROM xf_user");
    
    $model = new XenForo_Model_User();
    foreach ($userIds as $id)
    {
        print "Rebuilding $id<br/>\r\n";
        $model->rebuildCustomFieldCache($id);
    }
    
     
    jonatan.wb, Fuhrmann and infis like this.

Share This Page