<p>I had to do something similar to this in a plugin recently, <a href="https://craftcms.stackexchange.com/questions/9713/inserting-relationship-into-entries-field-with-plugin-not-saving-relation">Inserting Relationship Into Entries Field - Plugin Not Saving Relation</a>, I know it's users to an entry field but I would assume the methods are similar.</p>

<p>So you will need 3 things in order to save a users categories</p>

<p><strong>1. The <code>Craft\FieldModel</code> of the field which has the associations</strong></p>

<p><code>$field = craft()-&gt;fields-&gt;getFieldByHandle('propertyPreference');</code></p>

<p><strong>2. The source, in this case the matrix block that has the field</strong></p>

<p><code>$source = craft()-&gt;matrix-&gt;getBlockById(&lt;someId&gt;);</code></p>

<p><strong>3. An array of element id's you wish to relate to the field</strong></p>

<p><code>$relations = $user-&gt;propertyPreference-&gt;ids();</code> </p>

<p>Once you have those you can edit the <code>$relations</code> array however you see fit and then simply save the relations back</p>

<p><code>craft()-&gt;relations-&gt;saveRelations($field , $source, $relations);</code></p>

<p><a href="http://buildwithcraft.com/classreference/services/RelationsService#saveRelations-detail" rel="nofollow noreferrer">You can find out more about the save relations function in the class reference</a></p>

<p>As for which table, I would assume they are in <code>craft_relations</code></p>

CC BY-SA 3.0
2015-05-31T09:13:52.647Z
2015-05-31T09:13:52.647Z
2017-04-13T12:51:00.160Z
Alec Ritson
<p>I have some categories, and with the use of a category field a user can select these from within his / her profile.</p>

<p>Which is shown in the image:</p>

<p><img src="https://i.sstatic.net/7jHWe.png" alt="enter image description here"></p>

<p>In my Twig template I can get the user selections out using the following code.</p>

<pre><code>{% set property_prefs = craft.categories({
    relatedTo: { sourceElement: currentUser, field: "propertyPreference" },
    limit:     null
}) %}
</code></pre>

<p>But what I want to know is how can I programmatically save a user selection using the API from within my plugin.</p>

<p>Also, which table are these stored in?</p>

<plugin-development><matrix><users><categories><relations>
Save a selected category for a user (using a plugin)
# Save a selected category for a user (using a plugin)

(Asked by: username_0 on 2015-05-29)

I have some categories, and with the use of a category field a user can select these from within his / her profile.
Which is shown in the image:

In my Twig template I can get the user selections out using the following code.
{% set property_prefs = craft.categories({
    relatedTo: { sourceElement: currentUser, field: "propertyPreference" },
    limit:     null
}) %}

But what I want to know is how can I programmatically save a user selection using the API from within my plugin.
Also, which table are these stored in?

**Tags:** plugin-development, matrix, users, categories, relations

---

## 1 answers

### Answer by username_1 on 2015-05-31. Score: 1 | Accepted answer

I had to do something similar to this in a plugin recently, [Inserting Relationship Into Entries Field - Plugin Not Saving Relation](https://craftcms.stackexchange.com/questions/9713/inserting-relationship-into-entries-field-with-plugin-not-saving-relation), I know it's users to an entry field but I would assume the methods are similar.

So you will need 3 things in order to save a users categories

**1. The `Craft\FieldModel` of the field which has the associations**

`$field = craft()->fields->getFieldByHandle('propertyPreference');`

**2. The source, in this case the matrix block that has the field**

`$source = craft()->matrix->getBlockById();`

**3. An array of element id's you wish to relate to the field**

`$relations = $user->propertyPreference->ids();`

Once you have those you can edit the `$relations` array however you see fit and then simply save the relations back

`craft()->relations->saveRelations($field , $source, $relations);`

[You can find out more about the save relations function in the class reference](http://buildwithcraft.com/classreference/services/RelationsService#saveRelations-detail)

As for which table, I would assume they are in `craft_relations`


---

