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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

Group question: AddToGroupByName


Go to End


5 Posts   2275 Views

Avatar
dio5

Community Member, 501 Posts

26 September 2007 at 11:25pm

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...

Avatar
dio5

Community Member, 501 Posts

27 September 2007 at 12:57am

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.

Avatar
Sean

Forum Moderator, 922 Posts

29 October 2007 at 6:57pm

Edited: 29/10/2007 7:24pm

Hi there,

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

EDIT: Ticket created - http://open.silverstripe.com/ticket/1562

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
         $member->Groups()->add($group);
      }
   } else {
      // otherwise, create a new group
      $group = new Group();
      $group->Code = 'my-users';
      $group->write();
      // add the member to the group
      $member->Groups()->add($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.

Cheers,
Sean

Avatar
dio5

Community Member, 501 Posts

29 October 2007 at 10:01pm

Yeah,

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...

http://www.silverstripe.com/general-discussion/flat/2594?showPost=7649

Avatar
Sean

Forum Moderator, 922 Posts

30 October 2007 at 3:15pm

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?

Sean