Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Group question: AddToGroupByName

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

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

Page: 1
Go to End
Author Topic: 1882 Views
  • dio5
    Avatar
    Community Member
    501 Posts

    Group question: AddToGroupByName Link to this post

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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Group question: AddToGroupByName Link to this post

    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.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Group question: AddToGroupByName Link to this post

    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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Group question: AddToGroupByName Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Group question: AddToGroupByName Link to this post

    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

    1882 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.