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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Changing Member / Groups relationship to has_one / has_many?

Go to End

6 Posts   1337 Views


Community Member, 49 Posts

30 July 2011 at 8:46pm

Hi all,

Trying to figure out how to change the member / group relationship.

Essentially, I'm wanting to force a member to only be allowed to be a part of one group.

The reason behind this is because I'm using the member profiles module, and when I setup some groups that users can choose to belong to when registering, a CheckboxSetField is supplied so a user can select multiple groups.

In this particular instance, I'm wanting registrants to only be able to choose one group. For example, they can be a "Doctor" or a "Nurse". They shouldn't be able to belong to both.

The CheckboxSetField is pulled using:

$memberFields = Member::currentUser()->getMemberFormFields();

Which makes me think I need to enforce a has_one / has_many relationship in order to be able to pull a DropdownField instead. But maybe I'm wrong making that assumption.

The end result i'm desiring is a drop down box of available groups instead of a checkboxset on the registration form, so if there is a more direct approach to achieving this I'm all ears...

Any ideas?


Community Member, 79 Posts

30 July 2011 at 9:35pm


Have you tried removing that field and then adding another one in? Or do you not have access to that bit of the code?



Community Member, 244 Posts

31 July 2011 at 3:02pm

Edited: 31/07/2011 3:02pm

You can customise the fields for the member profiles module by applying an extension to the Member class and using the updateMemberFormFields hook. Just removing the groups field and replacing it with a dropdown will change the form field. You might need to customise the saving logic a bit though.


Community Member, 49 Posts

31 July 2011 at 4:07pm

Thanks Colin and thanks aj - that has given me some food for thought. I'll report back with how I get on ;)


Community Member, 49 Posts

1 August 2011 at 5:28pm

Hi guys,

Thanks again for the direction. Got me where I needed to be with this, so fantastic.

To make this work, I created a separate DataObjectDecorator which uses the updateMemberFormFields hook to remove the existing groups field, and add a new empty DropdownField.

Then, in MemberProfilePage.php, I edited a couple of lines....

When setting the source of the Groups field, I updated setSource to:

$memberField->setSource($availableGroups->toDropdownMap("ID", "Title"));

Finally, as mentioned by aj, I had to update the saving logic. In getSettableGroupIdsFrom, I removed a foreach loop on the group value as Groups is now a single value.

There was probably a way to do this without having to edit MemberProfilePage.php directly, but hey it's working...


Community Member, 14 Posts

25 January 2012 at 12:07pm

can you share your code, I am keen to do the same thing