Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T01:49:24.670Z
Answers.Text: No problem.  Care to mark it as the answer? :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T01:33:45.403Z
Answers.LastActivityDate: 2014-11-05T01:33:45.403Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development><database><user-groups>
Title: Get Craft UserGroup by ID in Plugin
ThreadText: # 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](http://buildwithcraft.com/classreference/#craft.app.services) layer to get things like that.
  
  There is a [UserGroupsService](http://buildwithcraft.com/classreference/services/UserGroupsService), which has all sorts of handy methods for retrieving UserGroups like [getGroupById()](http://buildwithcraft.com/classreference/services/UserGroupsService#getGroupById-detail) and [getGroupByHandle()](http://buildwithcraft.com/classreference/services/UserGroupsService#getGroupByHandle-detail).
  
  You can invoke these from your plugin like:
  
  ```
  $userGroup = craft()->usergroups->getGroupById(3);
  $groupName = $userGroup->name;
  
  ```
  
  Note that these will return a [UserGroupModel](http://buildwithcraft.com/classreference/models/UserGroupModel) populated with your UserGroup information.
  
  > **Comments:**
  > **username_1**: No problem.  Care to mark it as the answer? :)  
  
  ---
  
  