<p>I don't know what your specific error is, but whenever I use</p>

<pre><code>Event::on(
\craft\elements\User::class,
\craft\elements\User::EVENT_AFTER_SAVE,
</code></pre>

<p>I need to provide the next parameter with a <code>\yii\base\Event</code> callback, not a <code>\craft\events\ModelEvent</code> one.</p>

<p>Maybe that's your case as well ?
 You can also use <code>\yii\base\ModelEvent</code>, but not the craft one, as the craft ModelEvent does not extend <code>\yii\base\Event</code></p>

CC BY-SA 4.0
2018-06-05T23:50:47.060Z
That did not resolve the issue. I will post an "answer" with more detail below as it will not fit in a comment.
Eric Snyder
CC BY-SA 4.0
2018-06-04T15:40:38.807Z
2018-06-04T15:40:38.807Z
dustfeather
<p>I have worked on this for days now. 
Using either:</p>

<blockquote>
  <p>\yii\base\Event</p>
</blockquote>

<p>or </p>

<blockquote>
  <p>ElementEvent</p>
</blockquote>

<p>Seems to register the event and it fires after a new user is entered.
That's a good tip though.</p>

<p>My code currently looks like this:</p>

<pre><code>Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent  $event) {
        if (Craft::$app-&gt;getUser()-&gt;getIsAdmin() == false){
            $loggedInUser = Craft::$app-&gt;getUser();
            $userGroupUser = new UserGroup_User();
            $loggedInUserGroup = $loggedInUser-&gt;getIdentity()-&gt;getGroups()[0]['id'];
            $newUserId =  $event-&gt;element['id'];
            $userGroupUser-&gt;groupId = $loggedInUserGroup;
            $userGroupUser-&gt;userId = $newUserId;
            $result = $userGroupUser-&gt;save();
         }
    }
);
</code></pre>

<p>Things I know:</p>

<ol>
<li><p>The code fires when I add a new user. I know this by placing a
breakpoint on any line in the callback function.</p></li>
<li><p>The callback code will NOT add a new row in the usergroups_users
table for the brand new entered user. Let's say that user name is
"Test1".</p></li>
<li><p>After I allow the entry process to finish Test1 now shows up in the users table as a user with ID #50. The brand new "Test1" does not show up in the usergroups_uers table even though the callback code executed.</p></li>
<li><p>Next if I change the callback code to use the user ID (50) of the newest user (Test1) that I just entered and then go and create a new user called test "Test2" the callback function will create a record for user "Test1" (user ID 50) will be entered into the usergroups_users table. I will see that record if I examine the usergroups_user table.</p></li>
</ol>

<p>Code:</p>

<pre><code>Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent  $event) {
        if (Craft::$app-&gt;getUser()-&gt;getIsAdmin() == false){
            $userGroupUser = new UserGroup_User();
            $userGroupUser-&gt;groupId = 1;
            $userGroupUser-&gt;userId = 50;
            $result = $userGroupUser-&gt;save();
         }
    }
);
</code></pre>

<p>It's like the insert for the usergroups_users is silently failing if the user was just entered. It could also be that something was not committed???</p>

<p>I am new to Craft so it could be a noobie error but this one has me stumped.</p>

CC BY-SA 4.0
2018-06-05T23:55:55.253Z
2018-06-05T23:55:55.253Z
Eric Snyder
<p>I'm not sure what your issue is, the following Code works as expected. Of course you should check if the element is an instance of <code>craft\elements\User</code> and the user is not already in the group.</p>

<pre><code>Event::on(
    craft\services\Elements::class,
    craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(craft\events\ElementEvent  $event) {
            $userGroupUser = new craft\records\UserGroup_User();
            $userGroupUser-&gt;groupId = 1;
            $userGroupUser-&gt;userId = $event-&gt;element-&gt;getId();
            if(!$userGroupUser-&gt;save()){
                Craft::error('Could not save group ' . Json::encode($userGroupUser-&gt;getErrors()));
            }
    }
);
</code></pre>

CC BY-SA 4.0
2018-06-06T07:41:56.663Z
2018-06-06T08:14:40.187Z
2018-06-06T08:14:40.187Z
Robin Schambach
<p>Having an issue with using the event \craft\elements\User::EVENT_AFTER_SAVE in a plug in. I am adding a new user to a group. My goal is to have the newly added user automatically added to the same user group as the logged in user. This is my code:</p>

<pre><code>Event::on(
    \craft\elements\User::class,
    \craft\elements\User::EVENT_AFTER_SAVE,
    function(ModelEvent  $event) {
        if (Craft::$app-&gt;getUser()-&gt;getIsAdmin() == false){
            $loggedInUser = Craft::$app-&gt;getUser();
            $userGroupUser = new UserGroup_User();
            $userGroupUser-&gt;groupId = $loggedInUser-&gt;getIdentity()-&gt;getGroups()[0]['id'];
            $userGroupUser-&gt;userId = $event-&gt;sender-&gt;id;
            $result = $userGroupUser-&gt;insert();
         }
    }
);
</code></pre>

<p>I have put a breakpoint on the code after the if statement and then checked the users table - the new user is not in the users table yet.</p>

<p>I would expect that the new user would be in the table at this point. Is this transaction being saved and executed later? If so is there something I can do to force the transaction to save the new user?</p>

<p>Is this possible a bug?</p>

<p>I do not get any error messages. Why?</p>

<p>EDIT: I should also mention that the usergroup has permission to add users in their own usergroup.</p>

CC BY-SA 4.0
2018-06-03T16:43:30.713Z
You'll need to use the `afterSaveElement` event https://github.com/craftcms/cms/blob/develop/src/services/Elements.php#L534
Robin Schambach
CC BY-SA 4.0
2018-06-03T19:28:53.333Z
Thanks @robin. Not making sense to me though. Here is the doc on the event I was using: [EVENT_AFTER_SAVE](https://docs.craftcms.com/api/v3/craft-base-element.html#event-after-save.). What does not make sense is the docs says "The event that is triggered after the element is saved". When I changed this to use the event: \craft\services\elements::EVENT_AFTER_SAVE_ELEMENT the event does not fire.
Eric Snyder
CC BY-SA 4.0
2018-06-03T19:31:58.957Z
It does fire. The users `afterSave` is still inside the transaction so if you break it up (via breakpoint) the user isn't inside the table at that moment. The event `AfterSaveElement` is fired after the transaction is done, so after the element is stored
Robin Schambach
<plugin-development><craft3>
Problem with \craft\elements\User::EVENT_AFTER_SAVE
# Problem with \craft\elements\User::EVENT_AFTER_SAVE

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

Having an issue with using the event \craft\elements\User::EVENT_AFTER_SAVE in a plug in. I am adding a new user to a group. My goal is to have the newly added user automatically added to the same user group as the logged in user. This is my code:
Event::on(
    \craft\elements\User::class,
    \craft\elements\User::EVENT_AFTER_SAVE,
    function(ModelEvent  $event) {
        if (Craft::$app->getUser()->getIsAdmin() == false){
            $loggedInUser = Craft::$app->getUser();
            $userGroupUser = new UserGroup_User();
            $userGroupUser->groupId = $loggedInUser->getIdentity()->getGroups()[0]['id'];
            $userGroupUser->userId = $event->sender->id;
            $result = $userGroupUser->insert();
         }
    }
);

I have put a breakpoint on the code after the if statement and then checked the users table - the new user is not in the users table yet.
I would expect that the new user would be in the table at this point. Is this transaction being saved and executed later? If so is there something I can do to force the transaction to save the new user?
Is this possible a bug?
I do not get any error messages. Why?
EDIT: I should also mention that the usergroup has permission to add users in their own usergroup.

**Tags:** plugin-development, craft3
> **Comments:**
> **username_1**: You'll need to use the `afterSaveElement` event https://github.com/craftcms/cms/blob/develop/src/services/Elements.php#L534  
> **username_0**: Thanks @robin. Not making sense to me though. Here is the doc on the event I was using: [EVENT_AFTER_SAVE](https://docs.craftcms.com/api/v3/craft-base-element.html#event-after-save.). What does not make sense is the docs says "The event that is triggered after the element is saved". When I changed this to use the event: \craft\services\elements::EVENT_AFTER_SAVE_ELEMENT the event does not fire.  
> **username_1**: It does fire. The users `afterSave` is still inside the transaction so if you break it up (via breakpoint) the user isn't inside the table at that moment. The event `AfterSaveElement` is fired after the transaction is done, so after the element is stored  

---

## 3 answers

### Answer by username_2 on 2018-06-04. Score: 1

I don't know what your specific error is, but whenever I use
Event::on(
\craft\elements\User::class,
\craft\elements\User::EVENT_AFTER_SAVE,

I need to provide the next parameter with a \yii\base\Event callback, not a \craft\events\ModelEvent one.
Maybe that's your case as well ?
 You can also use \yii\base\ModelEvent, but not the craft one, as the craft ModelEvent does not extend \yii\base\Event

> **Comments:**
> **username_0**: That did not resolve the issue. I will post an "answer" with more detail below as it will not fit in a comment.  

---

### Answer by username_0 on 2018-06-05. Score: 0

I have worked on this for days now. 
Using either:

\yii\base\Event

or 

ElementEvent

Seems to register the event and it fires after a new user is entered.
That's a good tip though.
My code currently looks like this:
Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent  $event) {
        if (Craft::$app->getUser()->getIsAdmin() == false){
            $loggedInUser = Craft::$app->getUser();
            $userGroupUser = new UserGroup_User();
            $loggedInUserGroup = $loggedInUser->getIdentity()->getGroups()[0]['id'];
            $newUserId =  $event->element['id'];
            $userGroupUser->groupId = $loggedInUserGroup;
            $userGroupUser->userId = $newUserId;
            $result = $userGroupUser->save();
         }
    }
);

Things I know:

The code fires when I add a new user. I know this by placing a
breakpoint on any line in the callback function.
The callback code will NOT add a new row in the usergroups_users
table for the brand new entered user. Let's say that user name is
"Test1".
After I allow the entry process to finish Test1 now shows up in the users table as a user with ID #50. The brand new "Test1" does not show up in the usergroups_uers table even though the callback code executed.
Next if I change the callback code to use the user ID (50) of the newest user (Test1) that I just entered and then go and create a new user called test "Test2" the callback function will create a record for user "Test1" (user ID 50) will be entered into the usergroups_users table. I will see that record if I examine the usergroups_user table.

Code:
Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent  $event) {
        if (Craft::$app->getUser()->getIsAdmin() == false){
            $userGroupUser = new UserGroup_User();
            $userGroupUser->groupId = 1;
            $userGroupUser->userId = 50;
            $result = $userGroupUser->save();
         }
    }
);

It's like the insert for the usergroups_users is silently failing if the user was just entered. It could also be that something was not committed???
I am new to Craft so it could be a noobie error but this one has me stumped.


---

### Answer by username_1 on 2018-06-06. Score: 0

I'm not sure what your issue is, the following Code works as expected. Of course you should check if the element is an instance of craft\elements\User and the user is not already in the group.
Event::on(
    craft\services\Elements::class,
    craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(craft\events\ElementEvent  $event) {
            $userGroupUser = new craft\records\UserGroup_User();
            $userGroupUser->groupId = 1;
            $userGroupUser->userId = $event->element->getId();
            if(!$userGroupUser->save()){
                Craft::error('Could not save group ' . Json::encode($userGroupUser->getErrors()));
            }
    }
);


---

