An explanation on user_save() works I was writing on a module for drupal and i had to update some fields in the user-object. I first tried this code:

$account = user_load($user-&gt;uid); // Loading account
$edit = array(
  'gitlab_refresh_token' =&gt; $response_data['refresh_token'],
);
user_save($account, $edit);


This did not update the the field.

Then i found this code on the Internet:

$existingUser = user_load($user-&gt;uid);
$existingUser-&gt;data['gitlab_refresh_token'] = $response_data['refresh_token'];
user_save((object) array('uid' =&gt; $existingUser-&gt;uid), (array) $existingUser);


This code works. But the question is why?   

The documentation for user_save() says, that $edit just needs to contain an array with key/value pairs of what you want to change and it will be put in the user's data field,
and that is what i did, right?
The first snippet doesn't work because Drupal doesn't use any 'gitlab_refresh_token' array index, and it doesn't know any field with that name. If you were trying to change the value of a field added with the field UI, the array needs to have a different structure, to be saved.

The second snippet works because Drupal effectively uses that array index, as also said in the documentation.


  Key / value pairs added to the $edit['data'] will be serialized and saved in the {users.data} column.


// Prepopulate $edit['data'] with the current value of $account-&gt;data.
// Modules can add to or remove from this array in hook_user_presave().
if (!empty($account-&gt;data)) {
  $edit['data'] = !empty($edit['data']) ? array_merge($account-&gt;data, $edit['data']) : $account-&gt;data;
}