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?


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.

  • No problem. Care to mark it as the answer? :)
    – Brad Bell
    Nov 5 '14 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.