Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.


Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Group question: AddToGroupByName


5 Posts   1895 Views


26 September 2007 at 11:25pm Community Member, 501 Posts

I noticed the forum uses the method

Group::addToGroupByName($member, 'forum-members');

Anywhere I can read about this? I searched the docu, found nothing.
Tried this function myself, but didn't work... Member wasn't added to group...

Any hints much appreciated, .. and on my other questions too...


27 September 2007 at 12:57am Community Member, 501 Posts

I found the reason why it is not working.

Apparently, when you call it you shouldn't specify the group's name (title) but the group's code.
There's one big flaw however: making a group in the backend always gives the same code: 'new-group' whatever the name you give it may be.

Not really getting this, why isn't set properly?

Either AddToGroupByName should use the name of the group (title) or while creating the group in the backend the code should be changed properly as well, but calling every group 'new-group' makes 'AddToGroupByName' actually redundant, not?

A bug?

I hope changing the code in the DB manually will help, but I can't believe this is the way it's supposed to be.


29 October 2007 at 6:57pm (Last edited: 29 October 2007 7:24pm), Forum Moderator, 921 Posts

Hi there,

Code seems to be broken, perhaps. I'll create a ticket, or find out if there's already one.

EDIT: Ticket created - [url][/url]

You might do this instead:

// check if member exists, first
if($member = Member::currentUser()) {
// look for current group
if($group = DataObject::get_one('Group', "Code = 'my-users'")) {
// check this member isn't in the group
if(!$member->isInGroup($group->ID)) {
// add the member to the group
} else {
// otherwise, create a new group
$group = new Group();
$group->Code = 'my-users';
// add the member to the group

This checks if the member exists, adds the member to a current group it finds in the database. If that group doesn't exist, create a new one with that code value, and add it into the system.



29 October 2007 at 10:01pm Community Member, 501 Posts


the AddToGroupByName method seems very useful though, if the 'code' field would follow the title field.

But there seems to be some contradiction: in the other tread Jez just said 'code' is deprecated...


30 October 2007 at 3:15pm Forum Moderator, 921 Posts

AFAIK the only way to safely add a user to a group is using code, how does one add a member to a group using permissions?

Anyone else know?