<p>The user has already been saved at this point and is available as <code>$event-&gt;params['user']</code>. If you modify data and save again, however, you will need to be careful of creating an endless loop — not sure what you are trying to do. In this case you should be ok because of the "IsNewUser" conditional.</p>

<pre><code>public function init() 
{
    parent::init();
    craft()-&gt;on('users.saveUser', function(Event $event) {

        if ($event-&gt;params['isNewUser']) {

            $user = $event-&gt;params['user'];
            // update default fields
            $user-&gt;firstName = "Bob";
            // update custom fields
            $user-&gt;getContent()-&gt;setAttributes(array(
                'phone' =&gt; "222-222-5555"
            ));
            // save user
            $success = craft()-&gt;users-&gt;saveUser($user);

            if (!$success) {
                MyPlugin::log('Couldn’t update user "'.$user-&gt;username.'"', LogLevel::Error);
            } else {
                MyPlugin::log('Successfully updated user "'.$user-&gt;username.'"', LogLevel::Info);
            }
        }

    });
}
</code></pre>

CC BY-SA 3.0
2015-01-31T00:57:07.320Z
2015-01-31T00:57:07.320Z
Douglas McDonald
<p>I have the following code in my plugin.</p>

<pre><code>public function init() 
{
    parent::init();

    craft()-&gt;on('users.saveUser', function(Event $event) {

    // only fire if new user
    if ($event-&gt;params['isNewUser']) {

        // business logic
        // how do i save result to custom user fields etc...

    }         
 });
}
</code></pre>

<p>I guess my question is whether the userRecod has already saved at this point? If so do I need to create another user object, make changes and call .save again? Or is the user still available to me?   </p>

<plugins><usermodel>
Processing Business Logic on user.saveUser
# Processing Business Logic on user.saveUser

(Asked by: username_0 on 2015-01-31)

I have the following code in my plugin.
public function init() 
{
    parent::init();

    craft()->on('users.saveUser', function(Event $event) {

    // only fire if new user
    if ($event->params['isNewUser']) {

        // business logic
        // how do i save result to custom user fields etc...

    }         
 });
}

I guess my question is whether the userRecod has already saved at this point? If so do I need to create another user object, make changes and call .save again? Or is the user still available to me?

**Tags:** plugins, usermodel


---

## 1 answers

### Answer by username_1 on 2015-01-31. Score: 2 | Accepted answer

The user has already been saved at this point and is available as $event->params['user']. If you modify data and save again, however, you will need to be careful of creating an endless loop — not sure what you are trying to do. In this case you should be ok because of the "IsNewUser" conditional.
public function init() 
{
    parent::init();
    craft()->on('users.saveUser', function(Event $event) {

        if ($event->params['isNewUser']) {

            $user = $event->params['user'];
            // update default fields
            $user->firstName = "Bob";
            // update custom fields
            $user->getContent()->setAttributes(array(
                'phone' => "222-222-5555"
            ));
            // save user
            $success = craft()->users->saveUser($user);

            if (!$success) {
                MyPlugin::log('Couldn’t update user "'.$user->username.'"', LogLevel::Error);
            } else {
                MyPlugin::log('Successfully updated user "'.$user->username.'"', LogLevel::Info);
            }
        }

    });
}


---

