Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How do I get all Members from a group into an array?

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

Page: 1
Go to End
Author Topic: 3253 Views
  • baba-papa
    Avatar
    Community Member
    279 Posts

    How do I get all Members from a group into an array? Link to this post

    Hi,

    this is tricking me for 2 hours now. I can´t find any solution and I´m feeling like an idiot.
    For a few weeks I´m tweeking the PrivatMessaging-Module. To send a message, you choose a nickname from a dropdown menu:

    //Nicknames of all members is passed into an array
          $members = DataObject::get("Member", "", "Nickname");
          $allMembers = array();
          foreach($members as $member) {
    $allMembers[$member->ID] = htmlentities($member->Nickname);
          }
          $me = Member::currentUser();
          return new Form($this, "PostMessageForm", new FieldSet(
             new ReadonlyField("From", _t('PrivateMessagePage.FROM', 'From'), "$me->Nickname"),
             new DropdownField("ToID", _t('PrivateMessagePage.CHOOSERECEIVER', 'Choose receiver:'), $allMembers),
             new TextField("Subject", _t('PrivateMessagePage.SUBJECT', 'Subject')),
             new TextareaField("Body", _t('PrivateMessagePage.TEXT', 'Text'))


    The thing is, I want only members from the "forum members" group to appear in that menu. Members and groups have a many many relation. For that reason the group isn´t a class property of a member. The relation between members an groups is saved in an own database table. This means that I can´t use the method DataObject::get() to solve this issue.

    I appreciate any help or hint.

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: How do I get all Members from a group into an array? Link to this post

    I solved this myself. There is a method inGroup() in the Members.php. It is passed the Code of the Membergroup:

          $members = DataObject::get("Member", "", "Nickname");
          $allMembers = array();
          foreach($members as $member) {
    //Only Members of the forum-members group should appear in the $allMembers array
    if($member->inGroup("forum-members")){
    $allMembers[$member->ID] = htmlentities($member->Nickname);
    }
          }

    Sometimes it´s so easy ;)

  • Cuba
    Avatar
    Community Member
    12 Posts

    Re: How do I get all Members from a group into an array? Link to this post

    A more "proper" and more efficient way is to do the search in the DB query like so:

    DataObject::get("Member","Group_Members.GroupID=2","Created","LEFT JOIN `Group_Members` ON `Member`.`ID` = `Group_Members`.`MemberID` LEFT JOIN `Group` ON `Group`.`ID` = `Group_Members`.`GroupID`");

    I choose groupID "2" in this ex, but you get the idea...

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: How do I get all Members from a group into an array? Link to this post

    Thanks for Your help, cuba. I´m not that familiar with SQL yet, thats why I avoid working with SQL strings. I will try your idea next time and SQL will be my next subject to study.

  • MonkeyBen
    Avatar
    Community Member
    25 Posts

    Re: How do I get all Members from a group into an array? Link to this post

    I use this method: -

       $group = DataObject::get_one("Group", "Code = 'groupcode'");
       $records = $group->Members();

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