<p>Usually, its very easy to pull data from a relational fieldtype (Categories, Entries, Assets etc). E.g. if your Categories field handle is <code>memberGroups</code>, you can simply do this:</p>

<pre><code>$categories = $user-&gt;memberGroups;
</code></pre>

<p>However, your Categories field handle is <code>groups</code>, which conflicts with the built-in UserModel attribute <code>groups</code>, used to pull any user groups the user is a member of – this means that calling <code>$user-&gt;groups</code> will give you an array of UserGroupModel instances, ignoring your Categories field.</p>

<p>Also, the <code>$user-&gt;getContent()-&gt;getAttribute('groups');</code> method doesn't appear to work for relational fields.</p>

<p>The easy answer: I'd suggest you simply change the field handle for your Categories field to something else – for example <code>memberGroups</code>. Don't worry, you won't lose any data if you do. Then you can just do <code>$categories = $user-&gt;memberGroups;</code>.</p>

<p><strong>If you can't or wont change the field handle, here's a workaround:</strong></p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;relatedTo = array(
    'sourceElement' =&gt; $user,
    'field' =&gt; 'groups'
);
$categories = $criteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-04-06T15:20:06.187Z
Hi Mats - Sorry for confusing you, the "groups" is the handle of a custom category field type I assigned to the users field within the CP users settings fields page (/admin/settings/users/fields). Your code returns the user groups  that were created at /admin/settings/users, if the user w/ that id is assigned any of those groups. I am looking for the custom field type: "groups"
Brad
CC BY-SA 3.0
2016-04-06T15:37:48.743Z
If I do: $user->getContent()->occupation; it shows the occupation (is custom field type plain text) field data entered. But If I do $user->getContent()->groups; its blank. The groups field shows up within Craft\ContentModel Object
(
    [_requiredFields:Craft\ContentModel:private] => 
    [_attributeConfigs:Craft\ContentModel:private] => Array
Brad
CC BY-SA 3.0
2016-04-06T15:38:04.713Z
It prints out the group in that array:

            [groups] => Array
                (
                    [model] => 
                    [column] => text
                    [type] => mixed
                    [label] => Groups
                )
Brad
CC BY-SA 3.0
2016-04-06T16:30:25.077Z
I can get the info if I do a query of something like: select craft_content.title from craft_relations where sourceId = $id join craft_content on craft_relations.targetId = craft_content.elementId; But I want to do this using the craft services provided.
Brad
CC BY-SA 3.0
2016-04-06T17:12:45.043Z
Aaah, right... You're absolutely correct, `$user->groups` will output any groups (UserGroupModels) the user is in. If your field had a different handle, e.g. `groupCategories`, my original answer would work (i.e. `$user->groupCategories`). It's actually a bit weird that Craft allows `groups` as a field handle; pretty much every other built-in attribute for any of the element models is a reserved word and can't be used for field handles. Maybe this is an oversight on P&T's behalf, you might want to shoot them an email at support@craftcms.com.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-04-06T17:26:12.550Z
@Brad Just edited my answer, including a workaround if you don't want to change the field handle. Hope it works out!
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-04-06T03:51:37.893Z
2016-04-06T17:29:33.490Z
2016-04-06T17:29:33.490Z
Mats Mikkel Rummelhoff
<p>I am trying to capture the data that is selected when a user selects categories within a categories field type (handle: groups) within the Control Panel profile tab.</p>

<p>I was looking thru the Fields, Categories and Content services documentation in the craft docs section and I am still unsure.</p>

<p>I noticed when I did a var_dump of $user:</p>

<pre><code>$user = UserModel::populateModel(UserRecord::model()-&gt;findById($id));
</code></pre>

<p>It displays the field 'groups' within the _attributeConfigs, but does not show up in private '_attributes' (Craft\BaseModel)</p>

<p>The other fields do show up, for example, occupation shows up within both the _attributeConfigs (its a string) and shows up in private '_attributes' (Craft\BaseModel) with a value of 'Teacher'.</p>

<p>I am unsure how to get the data the user selected when selecting the categories while updating their profile within the control panel.</p>

<plugin-development><categories><field><usermodel>
Trying to capture categories field type data in user profile save/update
# Trying to capture categories field type data in user profile save/update

(Asked by: username_0 on 2016-04-03)

I am trying to capture the data that is selected when a user selects categories within a categories field type (handle: groups) within the Control Panel profile tab.
I was looking thru the Fields, Categories and Content services documentation in the craft docs section and I am still unsure.
I noticed when I did a var_dump of $user:
$user = UserModel::populateModel(UserRecord::model()->findById($id));

It displays the field 'groups' within the _attributeConfigs, but does not show up in private '_attributes' (Craft\BaseModel)
The other fields do show up, for example, occupation shows up within both the _attributeConfigs (its a string) and shows up in private '_attributes' (Craft\BaseModel) with a value of 'Teacher'.
I am unsure how to get the data the user selected when selecting the categories while updating their profile within the control panel.

**Tags:** plugin-development, categories, field, usermodel

---

## 1 answers

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

Usually, its very easy to pull data from a relational fieldtype (Categories, Entries, Assets etc). E.g. if your Categories field handle is memberGroups, you can simply do this:
$categories = $user->memberGroups;

However, your Categories field handle is groups, which conflicts with the built-in UserModel attribute groups, used to pull any user groups the user is a member of – this means that calling $user->groups will give you an array of UserGroupModel instances, ignoring your Categories field.
Also, the $user->getContent()->getAttribute('groups'); method doesn't appear to work for relational fields.
The easy answer: I'd suggest you simply change the field handle for your Categories field to something else – for example memberGroups. Don't worry, you won't lose any data if you do. Then you can just do $categories = $user->memberGroups;.
If you can't or wont change the field handle, here's a workaround:
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->relatedTo = array(
    'sourceElement' => $user,
    'field' => 'groups'
);
$categories = $criteria->find();

> **Comments:**
> **username_0**: Hi Mats - Sorry for confusing you, the "groups" is the handle of a custom category field type I assigned to the users field within the CP users settings fields page (/admin/settings/users/fields). Your code returns the user groups  that were created at /admin/settings/users, if the user w/ that id is assigned any of those groups. I am looking for the custom field type: "groups"  
> **username_0**: If I do: $user->getContent()->occupation; it shows the occupation (is custom field type plain text) field data entered. But If I do $user->getContent()->groups; its blank. The groups field shows up within Craft\ContentModel Object (     [_requiredFields:Craft\ContentModel:private] =>      [_attributeConfigs:Craft\ContentModel:private] => Array  
> **username_0**: It prints out the group in that array:              [groups] => Array                 (                     [model] =>                      [column] => text                     [type] => mixed                     [label] => Groups                 )  
> **username_0**: I can get the info if I do a query of something like: select craft_content.title from craft_relations where sourceId = $id join craft_content on craft_relations.targetId = craft_content.elementId; But I want to do this using the craft services provided.  
> **username_1**: Aaah, right... You're absolutely correct, `$user->groups` will output any groups (UserGroupModels) the user is in. If your field had a different handle, e.g. `groupCategories`, my original answer would work (i.e. `$user->groupCategories`). It's actually a bit weird that Craft allows `groups` as a field handle; pretty much every other built-in attribute for any of the element models is a reserved word and can't be used for field handles. Maybe this is an oversight on P&T's behalf, you might want to shoot them an email at support@craftcms.com.  
> **username_1**: @Brad Just edited my answer, including a workaround if you don't want to change the field handle. Hope it works out!  

---

