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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

How do I get all Members from a group into an array?


Go to End


5 Posts   6108 Views

Avatar
baba-papa

Community Member, 279 Posts

13 July 2009 at 7:46am

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.

Avatar
baba-papa

Community Member, 279 Posts

14 July 2009 at 3:13am

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 ;)

Avatar
Cuba

Community Member, 12 Posts

24 August 2009 at 7:36am

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

Avatar
baba-papa

Community Member, 279 Posts

24 August 2009 at 8:01am

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.

Avatar
monkeyben

Community Member, 25 Posts

15 June 2011 at 4:00am

I use this method: -

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