Jump to:

1975 Posts in 1305 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » [Solved] Frontent form save many_many relation

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

Page: 1
Go to End
Author Topic: 2031 Views
  • teejay
    Avatar
    Community Member
    63 Posts

    [Solved] Frontent form save many_many relation Link to this post

    Hi struggle atm with a form which is implemented on the frontend. I want to save the formdata in an enquiry object which has a many_many relation to my groups object. And in return the groups object has as belongs_many_many relation to the enquiry object. So far so good. Now i create the form for the frontend and run into an issue with the Listboxfield. I cant get it to save the selected groups. How can i do that ??

    so my controller looks like that

    class EnquiryPage_Controller extends Page_Controller {
       function EnquiryForm() {

          $groups = DataObject::get("GroupHolder",""); // getting all pages for the groups
          $map = $groups->toDropDownMap();

          $fields = new FieldSet(
             new TextField('Title'),
             new TextField('Text'),
             new ListboxField('Groups','Groups',$map,$map[0],5,true)
          );

    // Create actions
    $actions = new FieldSet(
    new FormAction('doForm', 'Submit')
    );

    $validator = new RequiredFields('Title', 'Text');

    return new Form($this, 'EnquiryForm', $fields, $actions, $validator);
    }

    function doForm($data, $form) {
    $submission = new EnquiryObject();
        $form->saveInto($submission);
    $submission->write();

    Director::redirectBack();
    }

    }

    Would be awesome if someone could help me with my issue.

  • teejay
    Avatar
    Community Member
    63 Posts

    Re: [Solved] Frontent form save many_many relation Link to this post

    Because no one was able to help me with my question I figured it out by my self. Took me a while to get my head around it but at the end its logical.

    here my new doForm

    function doForm($data, $form) {
    $submission = new EnquiryObject();
       
        $form->saveInto($submission);

        $submission->write();

        $data = $form->getData();
        foreach($data['Groups'] as $key => $value){
           $dob = DataObject::get_one('Group',"ID = ".$value);
           $submission->Groups()->add($dob);
           unset($dob);
        }

    Director::redirectBack();
    }

    thats all you need to do to save many_many relation via a frontend form.

    Tee

  • jens
    Avatar
    Community Member
    2 Posts

    Re: [Solved] Frontent form save many_many relation Link to this post

    I also had some problems finding a way of adding many_many relations via the frontend!

    But the solution ist actually quite easy, once you find the "Class ComponentSet" http://api.silverstripe.org/default/ComponentSet.html

    Here my solution:

    class Team extends DataObject {
    static $singular_name = 'Team';
    static $plural_name = 'Teams';
    static $db = array (
    'Title' => 'Varchar(255)',
    );
    static $belongs_many_many = array (
    'Coworkers' => 'Coworker',
    );
    }

    function myTeamsForm() {
    $doTeam = DataObject::get("Team");
    $mapTeam = $doTeam ? $mapTeam = $doTeam->toDropdownMap('ID','Title') : array();
    $currentMember = DataObject::get_by_id("Coworker",Member::currentUser()->ID);
    $idList = $currentMember->Teams()->getIdList();
    $fields= new FieldSet(new CheckboxSetField('Teams','meine Teams', $mapTeam, $idList));

    // Create actions
    $actions = new FieldSet(
    new FormAction('doForm', 'Submit')
    );

    return new Form($this, 'myTeamsForm', $fields, $actions);
    }

    function doForm($data, $form) {
    $currentMember = DataObject::get_by_id("Coworker",Member::currentUser()->ID);
    $currentMember->Teams()->setByIDList($data['Teams']);
    Director::redirectBack();
    }

    Hope this helps!

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