I have tried to save a custom field of "user field" type which resulted in this error.

"Failed to set unsafe attribute"


    '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?


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

  • Thank you, your comments helped me but in Craft 3, I can now just do this. $order->"fieldname" = [Craft::$app->user->id]; – ravos Nov 12 '18 at 10:51
  • 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 – Robin Schambach Nov 12 '18 at 11:05
  • sure, will keep an eye on further use-cases. Thank you – ravos Nov 12 '18 at 12:40

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.