<p><code>$newUser-&gt;getContent()-&gt;id</code> will return the primary key from the <code>craft_content</code> table, not from the <code>craft_users</code> table.</p>

<p>You'll want to use <code>$newUser-&gt;id</code>, instead.</p>

CC BY-SA 3.0
2015-06-05T07:11:03.223Z
awesome, yeah that makes perfect sense now - knew it was something simple.
Darren
CC BY-SA 3.0
2015-06-03T15:06:11.923Z
2015-06-03T15:06:11.923Z
Brad Bell
<p>So I'm guessing this is just a basic error but I can't for the life of me figure it out.</p>

<p>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:
<code>craft()-&gt;userGroups-&gt;assignUserToGroups($newUser-&gt;getContent()-&gt;id, 11);</code></p>

<pre><code>public function actionSendSignup()
{
   $this-&gt;requirePostRequest();

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

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

   $user-&gt;setAttributes($data);
   $user-&gt;pending = true;

   $success = craft()-&gt;users-&gt;saveUser($user);

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


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

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

   return $this-&gt;returnJson($user-&gt;getContent());

}
</code></pre>

<p>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.</p>

<p>I'm guessing I'm misunderstanding the user id field?</p>

<plugin-development><users><user-groups>
UserId Not correct
# UserId Not correct

(Asked by: username_0 on 2015-06-03)

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?

**Tags:** plugin-development, users, user-groups

---

## 1 answers

### Answer by username_1 on 2015-06-03. Score: 1 | Accepted answer

`$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.

> **Comments:**
> **username_0**: awesome, yeah that makes perfect sense now - knew it was something simple.  

---

