<p>I have a user model that contains several Matrix fields, but for the sake of this question I'll just discuss one of those fields.</p>

<p><em>The field we'll discuss is a phoneMatrix.</em></p>

<p>The phoneMatrix data can come in from multiple sources. What I mean is, there is no one place where a user can add/update/delete phone numbers. There will be a user profile page, but other applications on the website will allow for phone numbers to be saved/added/deleted. Given this, it is not optimal for me to use the method:</p>

<pre><code>craft()-&gt;users-&gt;saveUser($userModel)
</code></pre>

<p>The reason it is not optimal is because, for whatever unholy reason, Craft assumes that if matrix data does not exist after being set from POST that the missing data should be deleted. This assumption is not conducive to our process, yes we could pull all phone numbers for each save/update but that's unnecessary overhead.</p>

<p>If anyone has advice on alternative ways to save user data that avoids this problem it would be very much appreciated. I'll be looking into it on my own and if I find an answer I'll make sure to post it!</p>

<p><em>Thanks in advance for any help!</em></p>

CC BY-SA 3.0
2015-04-17T17:14:56.307Z
Before saving the user, you might try loading the existing matrix data and then reassigning it (changing just the telephone of course). In other words set the values equal to the existing value; then save.
Douglas McDonald
CC BY-SA 3.0
2015-04-18T14:56:40.913Z
Yeah I thought about that but I'd rather not. 

I just wonder if there is a better way? I think the big question is; is this behavior happening at the matrix element level or higher up? If it's higher up then I can probably bypass the issue and use a low level save method. If it's at the element level then I'd probably have to write my own element type, one similar to a matrix. 

Has anyone out there solved this problem?
Matt V
<plugin-development><users><matrix>
Best way to save user data?
# Best way to save user data?

(Asked by: username_0 on 2015-04-17)

I have a user model that contains several Matrix fields, but for the sake of this question I'll just discuss one of those fields.
The field we'll discuss is a phoneMatrix.
The phoneMatrix data can come in from multiple sources. What I mean is, there is no one place where a user can add/update/delete phone numbers. There will be a user profile page, but other applications on the website will allow for phone numbers to be saved/added/deleted. Given this, it is not optimal for me to use the method:
craft()->users->saveUser($userModel)

The reason it is not optimal is because, for whatever unholy reason, Craft assumes that if matrix data does not exist after being set from POST that the missing data should be deleted. This assumption is not conducive to our process, yes we could pull all phone numbers for each save/update but that's unnecessary overhead.
If anyone has advice on alternative ways to save user data that avoids this problem it would be very much appreciated. I'll be looking into it on my own and if I find an answer I'll make sure to post it!
Thanks in advance for any help!

**Tags:** plugin-development, users, matrix
> **Comments:**
> **username_1**: Before saving the user, you might try loading the existing matrix data and then reassigning it (changing just the telephone of course). In other words set the values equal to the existing value; then save.  
> **username_0**: Yeah I thought about that but I'd rather not.   I just wonder if there is a better way? I think the big question is; is this behavior happening at the matrix element level or higher up? If it's higher up then I can probably bypass the issue and use a low level save method. If it's at the element level then I'd probably have to write my own element type, one similar to a matrix.   Has anyone out there solved this problem?  

---

## 0 answers

