Jump to:

23473 Posts in 18941 Topics by 2878 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 889 Views
  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Changing Member / Groups relationship to has_one / has_many? Link to this post

    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?

  • ccburns
    Avatar
    Community Member
    74 Posts

    Re: Changing Member / Groups relationship to has_one / has_many? Link to this post

    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

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Changing Member / Groups relationship to has_one / has_many? Link to this post

    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.

  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Re: Changing Member / Groups relationship to has_one / has_many? Link to this post

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

  • flipsidenz
    Avatar
    Community Member
    49 Posts

    Re: Changing Member / Groups relationship to has_one / has_many? Link to this post

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

  • DaveTh
    Avatar
    Community Member
    14 Posts

    Re: Changing Member / Groups relationship to has_one / has_many? Link to this post

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

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