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.

Form Questions /

[Solved] Frontent form save many_many relation


Reply


3 Posts   2214 Views

Avatar
teejay

Community Member, 63 Posts

26 January 2010 at 6:31pm

Edited: 27/01/2010 12:09pm

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.

Avatar
teejay

Community Member, 63 Posts

27 January 2010 at 12:08pm

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

Avatar
jens

Community Member, 2 Posts

2 April 2010 at 10:31pm

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" [url]http://api.silverstripe.org/default/ComponentSet.html[/url]

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!