1

I have a plugin that listens for the users.onSaveUser. So when users register, an account is created, and I am creating an entry that will contain extended profile information. I am attempting to create a user field on that newly created entry to relate the entry back to the user account.

MyPlugin.php

$entry = new EntryModel();
$entry->sectionId = 16;
$entry->typeId = 1;
$entry->enabled = false;
$entry->authorId = $user->id;

$entry->getContent()->title = craft()->request->getPost('firstName') . ' ' . craft()->request->getPost('lastName');

// userAccount is the handle of the user field.
$entry->setContent(['fields' => ['userAccount' => $user->id]]);  // nope.
$entry->setContentFromPost(['userAccount' => $user->id]);        // nope.

...

craft()->entries->saveEntry($entry);

Everything is coming across except the user field. I'm not sure what the field needs/is looking for to set it's value.

1

Relational fields are always stored as arrays, even if you are limiting them to only one entry so this should work:

$entry->setContentFromPost(['userAccount' => array($user->id)]);
| improve this answer | |
  • That was it. I knew I was close - just wasn't sure how to send the right "stuff" to set the field. Thank you! – Damon Dec 4 '15 at 12:14

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.