1

I'm building a new Drupal 7 and brought over the user table at the beginning of the project from another Drupal 7 site. All has been good with that except that when users save their profile, they get the following error, which I believe is related to our custom Avatar builder module. I am wondering what I can do to suspend the error message so that it doesn't show for now during our testing phase.

Notice: Object of class stdClass could not be converted to int in drupal_write_record() (line 7340

Here is the section of code from the .module that I believe that relates to this error:

/**
 * Implements hook_user_insert().
 */
function my_avatar_selection_user_insert(&$edit, $account, $category) {
  if (isset($account->picture)) {  
    $usage = new stdClass;
    $usage->uid = $account->uid;
    $usage->fid = $account->picture;
    drupal_write_record('my_avatar_selection_usage', $usage);
  }
}

/**
 * Implements hook_user_update().
 */
function my_avatar_selection_user_update(&$edit, $account, $category) {  
  if (isset($account->picture)) {  
    $usage = new stdClass;
    $usage->uid = $account->uid;
    $usage->fid = $account->picture;
    $has_rows = (bool) db_query_range('SELECT 1 FROM {my_avatar_selection_usage} WHERE uid = :uid', 0, 1, array(':uid' => $account->uid))->fetchField();
    if ($has_rows) {
      drupal_write_record('my_avatar_selection_usage', $usage, 'uid');
    }
    else {
      drupal_write_record('my_avatar_selection_usage', $usage);
    }
  }
}

The module works by saving the data from jcarousel into the data field of the user table and when the profile is saved, it creates a graphic that looks like the options the users selected. As far as I can tell, there are no issues with saving the avatar, it just generates this error. I took this module from the old website 7.50 so maybe there is a component I don't need on the new site 7.54.

2

The error is caused from the fact $account->picture is an object, and drupal_write_record() is trying to convert it to an integer, since the my_avatar_selection_usage defines fid as an integer value. See the following code used from user_save(), which the function invoking hook_user_insert().

  // Process picture uploads.
  if (!empty($account->picture->fid) && (!isset($account->original->picture->fid) || $account->picture->fid != $account->original->picture->fid)) {
    $picture = $account->picture;

    // Omissis

  }

As you see from that code, the correct code you should be using is the following one.

/**
 * Implements hook_user_insert().
 */
function my_avatar_selection_user_insert(&$edit, $account, $category) {
  if (isset($account->picture)) {  
    $usage = new stdClass;
    $usage->uid = $account->uid;
    $usage->fid = $account->picture->fid;
    drupal_write_record('my_avatar_selection_usage', $usage);
  }
}

/**
 * Implements hook_user_update().
 */
function my_avatar_selection_user_update(&$edit, $account, $category) {  
  if (isset($account->picture)) {  
    $usage = new stdClass;
    $usage->uid = $account->uid;
    $usage->fid = $account->picture->fid;
    $has_rows = (bool) db_query_range('SELECT 1 FROM {my_avatar_selection_usage} WHERE uid = :uid', 0, 1, array(':uid' => $account->uid))->fetchField();
    if ($has_rows) {
      drupal_write_record('my_avatar_selection_usage', $usage, 'uid');
    }
    else {
      drupal_write_record('my_avatar_selection_usage', $usage);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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