2

So I'm guessing this is just a basic error but I can't for the life of me figure it out.

I'm trying to create a new user, set some attributes and add them to a group I've already made. The following code all works but for the line: craft()->userGroups->assignUserToGroups($newUser->getContent()->id, 11);

public function actionSendSignup()
{
   $this->requirePostRequest();

   $user = new UserModel();
   $user->username  = craft()->request->getPost('memberEmail');
   $user->unverifiedEmail  = craft()->request->getPost('memberEmail');

   $data = [
       "firstName" => craft()->request->getPost('memberFirstName'),
       "lastName" => craft()->request->getPost('memberLastName'),
       "email" => craft()->request->getPost('memberEmail'),
   ];

   $user->setAttributes($data);
   $user->pending = true;

   $success = craft()->users->saveUser($user);

   if ($success) {
       craft()->search->indexElementAttributes($user);


       // Assign them to my user group 11
       craft()->userGroups->assignUserToGroups($newUser->getContent()->id, 11);

       // Update the Element Index
       craft()->search->indexElementAttributes($user);
   }

   return $this->returnJson($user->getContent());

}

The get id returns a number that appears right (it's a low integer), but I get a SQL error on adding the row to the join table users_usergroups and the userId doesn't match the user that was created in the system.

I'm guessing I'm misunderstanding the user id field?

1

$newUser->getContent()->id will return the primary key from the craft_content table, not from the craft_users table.

You'll want to use $newUser->id, instead.

| improve this answer | |
  • awesome, yeah that makes perfect sense now - knew it was something simple. – Darren Jun 5 '15 at 7:11

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.