Answers.Body: <p>I'm not sure if this is causing your error, but it's recommended to use <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow noreferrer">setContentFromPost()</a> or <a href="http://buildwithcraft.com/classreference/models/BaseModel#setAttribute-detail" rel="nofollow noreferrer">getContent()->setAttributes()</a> so that craft has a chance to prep the field values, as discussed in this answer, <a href="https://craftcms.stackexchange.com/a/6788/784">setContent() vs $entry->getContent()->foo</a>, and <a href="https://craftcms.stackexchange.com/a/4487/784">What is getContent()?</a>.</p>
  
  <pre><code>$user = craft()-&gt;userSession-&gt;getUser();
  $user-&gt;getContent()-&gt;setAttributes(array(
      'middleName' =&gt; "Dave",
  ));
  craft()-&gt;users-&gt;saveUser($user);
  </code></pre>
  
  <p>Although with that error it's possible that something else is going on in your plugin.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-22T20:37:33.373Z
Answers.LastActivityDate: 2015-02-22T20:37:33.373Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>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.</p>
  
  <p>Here is the code I'm using:</p>
  
  <pre><code>$user = craft()-&gt;userSession-&gt;getUser();
  $user-&gt;setContent(array('middleName' =&gt; "Dave"));
  craft()-&gt;users-&gt;saveUser($user);
  </code></pre>
  
  <p>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:</p>
  
  <p>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'</p>
  
  <p>If <strong>"saveUser"</strong> is truly used for creating and updating what am I doing wrong?</p>
  
Tags: <user-profile><usermodel>
Title: Update User Profile with Plugin
ThreadText: # Update User Profile with Plugin
  
  (Asked by: username_0 on 2015-02-22)
  
  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?
  
  **Tags:** user-profile, usermodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-02-22. Score: 3
  
  I'm not sure if this is causing your error, but it's recommended to use [setContentFromPost()](http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail) or [getContent()->setAttributes()](http://buildwithcraft.com/classreference/models/BaseModel#setAttribute-detail) so that craft has a chance to prep the field values, as discussed in this answer, [setContent() vs $entry->getContent()->foo](https://craftcms.stackexchange.com/a/6788/784), and [What is getContent()?](https://craftcms.stackexchange.com/a/4487/784).
  
  ```
  $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.
  
  
  ---
  
  