Query custom user field not working

Tommy

Member
Hi,
i had a script made a while back, which is not working anymore, the query goes like this:
PHP:
// Setup a new instance of XenForo so we can use the XenForo-database
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

$xenDb = XenForo_Application::get('db');

// Get members from XenForo database along
$reservedSlotsQuery = $xenDb->select();
$reservedSlotsQuery->from(array('ua' => 'xf_user_upgrade_active'), 'user_id')->from('xf_user_group', '')
        ->join('xf_user_upgrade', 'xf_user_upgrade.user_upgrade_id = ua.user_upgrade_id', 'title')
        ->join(array('u' => 'xf_user'), 'u.user_id = ua.user_id', array('username'))
        ->join(array('up' => 'xf_user_profile'), 'up.user_id = ua.user_id', array('custom_fields'))
        ->where('xf_user_group.user_group_id in (?)', $upgradeIdsToAllow);
   
$reservedSlotsStmt = $reservedSlotsQuery->query();
$reservedSlotsStmt->setFetchMode(Zend_Db::FETCH_OBJ);
$data = $reservedSlotsStmt->fetchAll();

// extract the data from the XenForoDB
foreach($data as $player)
{
    $fields = @unserialize($player->custom_fields);
    $playerInXenForo[$player->user_id]['originid'] = $fields['originid'];
    $playerInXenForo[$player->user_id]['platform'] = $fields['platform'];
}

This is supposed to get a custom user field, but looks like something changed after it was made, and how do i make it work again? :)

This is the error i get:
An exception occurred: Undefined index: originid in /home/****/public_html/*****_xenforo.php on line 73

XenForo_Application::handlePhpError() in /home/****/public_html/*****_xenforo.php at line 73

Line 73 is:
PHP:
$playerInXenForo[$player->user_id]['originid'] = $fields['originid'];
 
Last edited:
That would imply the user doesn't have a value/entry for that custom field.
 
That would imply the user doesn't have a value/entry for that custom field.
But every member has, i even have a check so they cannot register or save the profile if the field is not filled.

So, have anything changed since xenforo 1.3 regarding how to query for the user fields? :)
 
What could prevent the code from returning data from the custom field? Does anyone have any idea?
 
Okay, there seems to be something wrong with my database, not this script.

Script is working on an old xenforo 1.3 db, just copied my current db over to my dev environment, boom! Failing!
N8LrXXD.png


How do i fix this @Mike? Something must have changed in xenforo, since it is working on the 1.3 db.
 
Nothing has fundamentally changed. As mentioned, based on the error, this is indicating that there's no entry for this field for the user. What you're accessing is a cache value, rather than the canonical value, though it shouldn't matter. It's just something you need to adapt your code to handle more gracefully or to simply resolve the issue by providing a value.

If you can create a case to reproduce it (the cache value not containing values in the user field table) using the core XF code, then we'd be looking at a bug fix, but this isn't something we've had reported before.
 
Nothing has fundamentally changed. As mentioned, based on the error, this is indicating that there's no entry for this field for the user. What you're accessing is a cache value, rather than the canonical value, though it shouldn't matter. It's just something you need to adapt your code to handle more gracefully or to simply resolve the issue by providing a value.

If you can create a case to reproduce it (the cache value not containing values in the user field table) using the core XF code, then we'd be looking at a bug fix, but this isn't something we've had reported before.
This doesn't make any sense to me at all. How do i provide the value? I thought i already did? How do i get the canonical value?

Is there any way to downgrade xenforo? I need this to work...
 
Here's the direct output of the $data query:
Code:
    [0] => stdClass Object
        (
            [user_id] => 1
            [title] => Reservert slot 1 mnd (Løpende)
            [username] => TommyMiland
            [custom_fields] => a:13:{s:3:"aim";s:0:"";s:8:"facebook";s:0:"";s:7:"fornavn";s:5:"Tommy";s:5:"gtalk";s:0:"";s:3:"icq";s:0:"";s:8:"originid";s:11:"TommyMiland";s:8:"platform";s:2:"PC";s:5:"raptr";s:11:"TommyMiland";s:5:"skype";s:12:"tommy.miland";s:10:"twitchname";s:11:"TommyMiland";s:7:"twitter";s:0:"";s:5:"yahoo";s:0:"";s:18:"youtubeUserProfile";s:0:"";}
        )
 
ooookay! After a whole day, i finally got it working. :cautious:
L9EeLAJ.png


For reference, i did it like this:
PHP:
    foreach($data as $player)
    {
        $fields = unserialize($player->custom_fields);
        if (isset ($fields['originid']) && !empty ($fields['platform']))
        {
            $playerInXenForo[$player->user_id]['originid'] = $fields['originid'];
            $playerInXenForo[$player->user_id]['platform'] = $fields['platform'];
        }
    }
 
Back
Top Bottom