<p>You'll want to go through Craft's <a href="http://buildwithcraft.com/classreference/#craft.app.services" rel="nofollow">service</a> layer to get things like that.</p>

<p>There is a <a href="http://buildwithcraft.com/classreference/services/UserGroupsService" rel="nofollow">UserGroupsService</a>, which has all sorts of handy methods for retrieving UserGroups like <a href="http://buildwithcraft.com/classreference/services/UserGroupsService#getGroupById-detail" rel="nofollow">getGroupById()</a> and <a href="http://buildwithcraft.com/classreference/services/UserGroupsService#getGroupByHandle-detail" rel="nofollow">getGroupByHandle()</a>.</p>

<p>You can invoke these from your plugin like:</p>

<pre><code>$userGroup = craft()-&gt;usergroups-&gt;getGroupById(3);
$groupName = $userGroup-&gt;name;
</code></pre>

<p>Note that these will return a <a href="http://buildwithcraft.com/classreference/models/UserGroupModel" rel="nofollow">UserGroupModel</a> populated with your UserGroup information.</p>

CC BY-SA 3.0
2014-11-05T01:49:24.670Z
No problem.  Care to mark it as the answer? :)
Brad Bell
CC BY-SA 3.0
2014-11-05T01:33:45.403Z
2014-11-05T01:33:45.403Z
Brad Bell
<p>I'm trying to get a record from my database for a UserGroup (although I'm guessing this applies to a few scenarios really - anything that isn't a Craft Element.</p>

<p>I'm still new to plugin development so I could be approaching this entirely wrong but I'm pretty sure I don't want to be using <code>craft()-&gt;db-&gt;createCommand()</code> because it might break in a future version of craft when the schema changes and I also can't do it by using <code>craft()-&gt;elements-&gt;getCriteria(ElementType::UserGroup);</code> because it's not an element.</p>

<p>Is there a best practice for this?</p>

<plugin-development><database><user-groups>
Get Craft UserGroup by ID in Plugin
# Get Craft UserGroup by ID in Plugin

(Asked by: username_0 on 2014-11-04)

I'm trying to get a record from my database for a UserGroup (although I'm guessing this applies to a few scenarios really - anything that isn't a Craft Element.

I'm still new to plugin development so I could be approaching this entirely wrong but I'm pretty sure I don't want to be using `craft()->db->createCommand()` because it might break in a future version of craft when the schema changes and I also can't do it by using `craft()->elements->getCriteria(ElementType::UserGroup);` because it's not an element.

Is there a best practice for this?

**Tags:** plugin-development, database, user-groups


---

## 1 answers

### Answer by username_1 on 2014-11-05. Score: 2 | Accepted answer

You'll want to go through Craft's service layer to get things like that.
There is a UserGroupsService, which has all sorts of handy methods for retrieving UserGroups like getGroupById() and getGroupByHandle().
You can invoke these from your plugin like:
$userGroup = craft()->usergroups->getGroupById(3);
$groupName = $userGroup->name;

Note that these will return a UserGroupModel populated with your UserGroup information.

> **Comments:**
> **username_1**: No problem.  Care to mark it as the answer? :)  

---

