Trying to call user_save() won't work on my installation. Guessing why.

    $account = user_load( 10 );
    $edit = array( 'mail' => "exampla@example.com" );

This will cause this error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1302092562 ) in drupal_write_record() (line 6776 of /var/www/citius/includes/common.inc).

Any clues on how can I debug this? Thank you.

Comments

jorge.suarez’s picture

Sorry, that code is wrong. I forgot the user_save() call.

    $account = user_load( 10 );
    $edit = array( 'mail' => "exampla@example.com" );
    user_save($account, $edit);
jorge.suarez’s picture

As I suspected, this has something to do with profile fields. After receiving this error, all the profile fields of type Image are gone forever.

I'm reproducing now this error. Please take a look at this demo: http://www.panicoenelnucleo.com/drupaltest/drupal-7.0/

- Clean 7.0 install standard.
- Enabled PHP Filter.
- Disabled user picture. Added image profile field.
- Created static page http://www.panicoenelnucleo.com/drupaltest/drupal-7.0/node/1
- The content of this page:

    global $user;
    $account = user_load( $user->uid );
    $edit = array( 'mail' => "exampla@example.com" );
    user_save($account, $edit);

So, to reproduce this bug:

- Login as testuser1, password testpassword. You can also login as testuser2, testuser3 and testuser4.
- Visit http://www.panicoenelnucleo.com/drupaltest/drupal-7.0/node/1 (notice that e-mail is changed, no error is thrown)
- Add a picture in your profile.
- Visit again http://www.panicoenelnucleo.com/drupaltest/drupal-7.0/node/1
- Everything seems to be OK. Reload. Then you get these warnings:

    Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/includes/entity.inc).
    Notice: Trying to get property of non-object in file_field_presave() (line 221 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/modules/file/file.field.inc).
    Notice: Undefined property: stdClass::$uri in file_save() (line 575 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/includes/file.inc).

- Reload again. Then you get the warnings again, then the fatal error:

    Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/includes/entity.inc).
    Notice: Trying to get property of non-object in file_field_presave() (line 221 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/modules/file/file.field.inc).
    Notice: Undefined property: stdClass::$uri in file_save() (line 575 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/includes/file.inc).
    PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1302105551 ) in drupal_write_record() (line 6776 of /home/panico7/panicoenelnucleo.com/drupaltest/drupal-7.0/includes/common.inc).
jorge.suarez’s picture

Title: user_save() throws a PDOException » user_save() throws a PDOException when using Image profile fields
Issue tags: +profile, +cck fields

Changed title

jorge.suarez’s picture

Continued testing with this demo using testuser4 and vardumped some values. Definitely, user_save() is messing up with the Image fields. Can someone else more experienced test this?

neutecake’s picture

Same problem.

After calling the user_save my filefield data is been deleted.

neutecake’s picture

I've found a solution:

$existing_user = user_load($userid);

$edited_user = (array) $existing_user;

$edited_user['field_match_type'] = array(LANGUAGE_NONE => array(array('value' => $form_state['values']['match_type'])));

$user = user_save($existing_user, $edited_user);
jorge.suarez’s picture

Can you explain your workaround for this problem? What's the issue exactly? Maybe we can fix this on Drupal Core instead of workaround the problem.

Thank you,

neutecake’s picture

I think it isn't a bug.

The user_save function updates all fields from the user, if the field isn't in the arrah of the second argument ($edit) the field wil be emptied.

jorge.suarez’s picture

But user_load() should retrieve all the data. Why not retrieve file/image fields only?. Also, Drupal shouldn't throw a PDOException like that, so this is definitely a bug to me. Maybe I don't understand something.

windmaomao’s picture

I have the similar issue, try to empty the field before user_save, still not working.

kenorb’s picture

Priority: Normal » Minor
Issue summary: View changes

Version: 7.0-rc4 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.