3

What is the code to update a user profile?

This gets the user profile:

   $user = JFactory::getUser();
   $userid = $user->id;
   $profile = JUserHelper::getProfile($user->id);
   echo print_r($profile);

Now what do I type to update one of the profile fields please? Is it up to me to write it or is there a command to save the profile?

Thanks,

Mat

2 Answers 2

6

You can modify this code from Joomla Docs to fit your needs, depending on what you're trying to achieve:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
    $db->quoteName('profile_value') . ' = ' . $db->quote('Updating custom message for user 1001.'),
    $db->quoteName('ordering') . ' = 2'
);
// Conditions for which records should be updated.
$conditions = array(
    $db->quoteName('user_id') . ' = 42', 
    $db->quoteName('profile_key') . ' = ' . $db->quote('custom.message')
);
$query->update($db->quoteName('#__user_profiles'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->query();
1
  • So you are saying I have to write it myself?
    – Mat Kay
    Aug 20, 2014 at 5:03
6

This is how Joomla user profile plugins does

// Sanitize the date
$data['profile']['dob'] = $this->_date;

$db = JFactory::getDbo();
$query = $db->getQuery(true)
                ->delete($db->quoteName('#__user_profiles'))
                ->where($db->quoteName('user_id') . ' = ' . (int) $userId)
                ->where($db->quoteName('profile_key') . ' LIKE ' . $db->quote('profile.%'));
$db->setQuery($query);
$db->execute();

$tuples = array();
$order = 1;
foreach ($data['profile'] as $k => $v)
{
    $tuples[] = '(' . $userId . ', ' . $db->quote('profile.' . $k) . ', ' . $db->quote(json_encode($v)) . ', ' . $order++ . ')';
}

$db->setQuery('INSERT INTO #__user_profiles VALUES ' . implode(', ', $tuples));
$db->execute();

You can get reference from Root => plugins => user => profile => profile.php in onUserAfterSave function.

2
  • So you are saying I have to write it myself?
    – Mat Kay
    Aug 20, 2014 at 5:03
  • 2
    @MatKay : Yes, you need to do it yourself.
    – Gaurav
    Aug 20, 2014 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.