XF 2.1 How to get value of xf_user_profile.custom_fields using finder?

AndyB

Well-known member
How to get value of xf_user_profile.custom_fields using finder?

I currently do this with the following MySQL query:

PHP:
$custom_fields = $db->fetchOne("
SELECT custom_fields 
FROM xf_user_profile
WHERE user_id = ?
", $userId);

I then get the data with this code:

$customFieldsArray = json_decode($custom_fields, true);

Now when I try to use the finder:

PHP:
$finder = \XF::finder('XF:UserProfile');
$userProfile = $finder
    ->where('user_id', $userId)
    ->fetchOne();

and try to get the value using:

PHP:
$customFields = $userProfile->custom_fields;

I get back an object. How would I get the value of the custom_fields?

Thank you.
 
For some reason I'm not able to use it like an array.

When I use the following code:

PHP:
echo $user['custom_fields'];

I get the following error:

1564361818589.webp
 
Would you use that code on an array?

Probably not, it would just print Array.

The class does not implement __toString() so you can't echo it.

You can access the fields by just using it as an array: $customfields['fieldname'].
 
I thank you for your time, Kirby.

I was able to accomplish what I needed by using the finder on two other tables.
 
This doesnt work for my cronjob

Code:
            $finder = \XF::app()->finder('XFRM:ResourceItem');
            $resource = $finder->order('last_update','DESC')->fetchOne();

            $customfields = $resource->customfields;

            echo "<pre>";
            //print_r($customfields['adress']);
            print_r($customfields);
            echo "</pre>";


Is there any difference to the example above?
 
Back
Top Bottom