Answers.Body: <p>In order to set field values there is <code>setFieldValue</code> function</p>
  
  <pre><code>$element-&gt;setFieldValue('fieldHandle', [Craft::$app-&gt;getUser()-&gt;getIdentity()-&gt;id]);
  </code></pre>
  
  <p>All relation fields only accept the values as an array <code>[1]</code> rather than integer <code>1</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-12T10:51:35.103Z
Answers.Text: Thank you, your comments helped me but in Craft 3, I can now just do this.   $order->"fieldname" = [Craft::$app->user->id];
Answers.UserDisplayName: ravos
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-12T11:05:04.957Z
Answers.Text: No you can't. This may work in one specific scenario but it's highly suggested not to do this. You can only do this with `field:handle` otherwise the value won't be normalized
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-12T12:40:24.393Z
Answers.Text: sure, will keep an eye on further use-cases. Thank you
Answers.UserDisplayName: ravos
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-10T10:29:32.553Z
Answers.LastActivityDate: 2018-11-10T10:29:32.553Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have tried to save a custom field of "user field" type which resulted in this error.</p>
  
  <blockquote>
    <p>"Failed to set unsafe attribute"</p>
  </blockquote>
  
  <p>code:</p>
  
  <pre><code>$order-&gt;setAttributes(array(
      'operatorUid' =&gt; Craft::$app-&gt;user-&gt;id
  ));
  </code></pre>
  
  <p>Has anyone encountered a similar issue or can you let me know how to save a custom userType field to an order?</p>
  
Tags: <custom-field>
Title: saving user field type custom field - Failed to set unsafe attribute
ThreadText: # saving user field type custom field - Failed to set unsafe attribute
  
  (Asked by: username_0 on 2018-11-10)
  
  I have tried to save a custom field of "user field" type which resulted in this error.
  
  > "Failed to set unsafe attribute"
  
  code:
  
  ```
  $order->setAttributes(array(
      'operatorUid' => Craft::$app->user->id
  ));
  
  ```
  
  Has anyone encountered a similar issue or can you let me know how to save a custom userType field to an order?
  
  **Tags:** custom-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-10. Score: 3
  
  In order to set field values there is `setFieldValue` function
  
  ```
  $element->setFieldValue('fieldHandle', [Craft::$app->getUser()->getIdentity()->id]);
  
  ```
  
  All relation fields only accept the values as an array `[1]` rather than integer `1`
  
  > **Comments:**
  > **username_0**: Thank you, your comments helped me but in Craft 3, I can now just do this.   $order->"fieldname" = [Craft::$app->user->id];  
  > **username_1**: No you can't. This may work in one specific scenario but it's highly suggested not to do this. You can only do this with `field:handle` otherwise the value won't be normalized  
  > **username_0**: sure, will keep an eye on further use-cases. Thank you  
  
  ---
  
  