<p>Not totally sure, but I believe this may be because the UsersService actually caches UserModels fetched by <code>getUserById()</code> locally in a private class variable.</p>

<p>Instead of using <code>getUserById()</code>, try pulling the user with an ElementCriteriaModel:</p>

<pre><code>$existing = craft()-&gt;elements-&gt;getCriteria(ElementType::User, array(
    'id' =&gt; $userId,
))-&gt;first();
</code></pre>

CC BY-SA 3.0
2016-06-21T20:35:27.020Z
Yeah, there's some memoization of the new data going on before that event is fired is the problem.
Brad Bell
CC BY-SA 3.0
2016-06-21T20:18:41.000Z
2016-06-21T20:18:41.000Z
Mats Mikkel Rummelhoff
<p>I'm trying to run a quick check to see if any of my user's data has changed before saving them. However, I'm running into a situation where I can <strong>only access the new data</strong>.</p>

<p>I can't tell if this is a bug, or simply a misunderstanding of how it works...</p>

<pre class="lang-php prettyprint-override"><code>craft()-&gt;on('users.beforeSaveUser', function(Event $event) {

    // Only do this for existing users
    if(!$event-&gt;params['isNewUser'])
    {

        // Get ID of user about to be saved
        $userId = $event-&gt;params['user']-&gt;id;

        // Look up existing user data
        $existing = craft()-&gt;users-&gt;getUserById($userId);

        // Why does this contain the NEW data?
        Craft::dd($existing-&gt;myField);

    }

});
</code></pre>

<p>There may be a reasonable explanation as to why the pre-existing user data is not available. If that is the case, then <strong>how can I access the pre-existing data for this user?</strong></p>

<events><usermodel>
`onBeforeSaveUser` - Access existing user data
# `onBeforeSaveUser` - Access existing user data

(Asked by: username_0 on 2016-06-21)

I'm trying to run a quick check to see if any of my user's data has changed before saving them. However, I'm running into a situation where I can only access the new data.
I can't tell if this is a bug, or simply a misunderstanding of how it works...
craft()->on('users.beforeSaveUser', function(Event $event) {

    // Only do this for existing users
    if(!$event->params['isNewUser'])
    {

        // Get ID of user about to be saved
        $userId = $event->params['user']->id;

        // Look up existing user data
        $existing = craft()->users->getUserById($userId);

        // Why does this contain the NEW data?
        Craft::dd($existing->myField);

    }

});

There may be a reasonable explanation as to why the pre-existing user data is not available. If that is the case, then how can I access the pre-existing data for this user?

**Tags:** events, usermodel

---

## 1 answers

### Answer by username_1 on 2016-06-21. Score: 3 | Accepted answer

Not totally sure, but I believe this may be because the UsersService actually caches UserModels fetched by getUserById() locally in a private class variable.
Instead of using getUserById(), try pulling the user with an ElementCriteriaModel:
$existing = craft()->elements->getCriteria(ElementType::User, array(
    'id' => $userId,
))->first();

> **Comments:**
> **username_2**: Yeah, there's some memoization of the new data going on before that event is fired is the problem.  

---

