21491 Posts in 5783 Topics by 2621 members
General Questions
SilverStripe Forums » General Questions » Changing Member / Groups relationship to has_one / has_many?
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 689 Views |
-
Changing Member / Groups relationship to has_one / has_many?

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?
-
Re: Changing Member / Groups relationship to has_one / has_many?

30 July 2011 at 9:35pm
Mike,
Have you tried removing that field and then adding another one in? Or do you not have access to that bit of the code?
Cheers,
Colin -
Re: Changing Member / Groups relationship to has_one / has_many?

31 July 2011 at 3:02pm Last edited: 31 July 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.
-
Re: Changing Member / Groups relationship to has_one / has_many?

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 ;)
-
Re: Changing Member / Groups relationship to has_one / has_many?

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...
-
Re: Changing Member / Groups relationship to has_one / has_many?

25 January 2012 at 12:07pm
can you share your code, I am keen to do the same thing
| 689 Views | ||
|
Page:
1
|
Go to Top |


