1

I'm trying to create a plugin to create/update users' profile data. I figured out how to create a user but updating... not so much.

Here is the code I'm using:

$user = craft()->userSession->getUser();
$user->setContent(array('middleName' => "Dave"));
craft()->users->saveUser($user);

From what I understand the "saveUser" method is used for both creating and updating user data. Whenever I run the code I get the following error:

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-en_us' for key 'craft_content_elementId_locale_unq_idx'

If "saveUser" is truly used for creating and updating what am I doing wrong?

3

I'm not sure if this is causing your error, but it's recommended to use setContentFromPost() or getContent()->setAttributes() so that craft has a chance to prep the field values, as discussed in this answer, setContent() vs $entry->getContent()->foo, and What is getContent()?.

$user = craft()->userSession->getUser();
$user->getContent()->setAttributes(array(
    'middleName' => "Dave",
));
craft()->users->saveUser($user);

Although with that error it's possible that something else is going on in your plugin.

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.