XF 1.5 Custom User fields and external php queries...

l0wD

Member
Hi guys,

im working on an external script for xenforo where i read and write values into a custom user field. It all worked good for the last 3 custom fields i created. But now when i try to access the 4th custom field i just created, my php script tells me that it doesnt exist:

ErrorException: Undefined index: lastaction
#0 /path/script.php(114): XenForo_Application::handlePhpError(8, 'Undefined index...', '/path/......', 114, Array)

when i var_dump() the custom field isnt listed. but i can see and edit it in the admin menu.

and here is the crazy part:
when i write a value into it from inside the admin panel, the script suddenly finds it and works as expected.
is it normal that xenforo delays creation of the custom field for each user seperatly? how can i use (or initalize) the custom fields for all users so i can use the field even if i never wrote a value into it before?

i hope i expressed myself clear enough :)
with best regards
 
Last edited:
is it normal that xenforo delays creation of the custom field for each user seperatly?
Yes, this is totally normally. You're acting on a cache of the field values. The lack of any entry would indicate that the user has never set any value for it.

how can i use (or initalize) the custom fields for all users so i can use the field even if i never wrote a value into it before?
Short answer is you can't. You need to account for this in your PHP. isset() should be sufficient.
 
Yes, this is totally normally. You're acting on a cache of the field values. The lack of any entry would indicate that the user has never set any value for it.


Short answer is you can't. You need to account for this in your PHP. isset() should be sufficient.

mhh im saving data like timestamps of last earn time (coins) inside these custom fields. (theyre only moderator accessable)
but if theyre not set (got a check for empty fields) how can i write to them?
cant i just add the field to the array, reserialze custom fields and write them with datawriter?

and if not how should i go about coding such functionality? do i really have to use the xf_user sql table?
i really like the idea of doing it in custom fields cuz admins/mods can edit the values in them, without the need of an external interface.

thanks in advance ;)

i just found this threads:
https://xenforo.com/community/threads/force-updating-a-users-custom-user-field.33664/
https://xenforo.com/community/threads/default-value-of-custom-user-fields.35236/
till u awnser im gonna check on XenForo_Model_User::rebuildCustomFieldCache :)
 
Last edited:
mhh i just tried:

Code:
...
$pUserModel  = XenForo_Model::create("XenForo_Model_User");

//login
$iUserid = $pUserModel->validateAuthentication($i_strUser, $i_strPass, $strError);
if(!$iUserid)
{
...
}

//rebuild custom field cache
$pUserModel->rebuildCustomFieldCache($iUserid);
...

but its not updating the customfields, error stays the same. any idea why?

perhaps i rebuild the func from xf code?
Code:
    ///**
    // * Rebuilds the custom field cache for a specific user, using the canonical data.
    // *
    // * [USER=103277]@Param[/USER] integer $userId
    // */
    //public function rebuildCustomFieldCache($userId)
    //{
    //    $db = $this->_getDb();
    //    $cache = $this->_getFieldModel()->getUserFieldValues($userId);
    //    $db->update('xf_user_profile',
    //        array('custom_fields' => serialize($cache)),
    //        'user_id = '. $db->quote($userId)
    //    );
    //}

Edit:
When i var_dump($cache), return is completly empty?
Code:
$pFieldModel = XenForo_Model::create('XenForo_Model_UserField');
$cache = $pFieldModel->getUserFieldValues($iUserid);
tested some more and $cache is empty even if i use the correct user who has custom fields already updated. (save profile)
can u tell me why?
___
this also doesnt work:
Code:
$vCustomFields = unserialize($pUser['custom_fields']);


if (!isset($vCustomFields['lastearn']) )
{
    file_put_contents("log.txt", "lastearn doesnt exist\n", FILE_APPEND );

    $pDataWriter = XenForo_DataWriter::create('XenForo_DataWriter_User');
    $pDataWriter->setExistingData($iUserid);
    $pDataWriter->setCustomFields(array('lastearn' => 0));
    $pDataWriter->rebuildCustomFields();
    $pDataWriter->save();
    $pUserModel->rebuildCustomFieldCache($iUserid);


}
else
    file_put_contents("log.txt", "lastearn exists ".$vCustomFields['lastearn']."\n", FILE_APPEND );
 
Last edited:
Given that you have a particular set of custom data that you need, I would probably just create the additional columns/tables that you need so that you can more directly trigger the specific behavior that you rely on. It will likely be more straightforward to handle in most scenarios.

Custom fields can always have a state where there is no value for a user and thus it won't be present in a cache (or the user field values). This is a situation you would need to account for if you want to use the custom field system. If you use your own columns, you would have more direct control over how this works.
 
Back
Top Bottom