Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Multiple DataObjects (of the same type) on the same form...

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

Page: 1
Go to End
Author Topic: 703 Views
  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Multiple DataObjects (of the same type) on the same form... Link to this post

    Hello, I'm trying to work out how to show on a single form multiple instances on the same form, so...

    class ObjOne extends DataObject{
       static $db = array('ItemOne'=>'Text');
    }
    class ObjTwo extends DataObject{
       static $db = array('ItemTwo'=>'Text');
    }

    ...

    //I'd like to do the following to create the fields...
    $fields = new FieldSet();
    for($i=0;$i<5;$i++)
       $fields->merge(singleton('ObjOne')->getFrontendFields());
    for($i=0;$i<10;$i++)
       $fields->merge(singleton('ObjTwo')->getFrontendFields());

    ...

    //then do something like this in the form action to save them... (where $data is the data passed from the form)

    for($i=0;$i<5;$i++){
       $doObjOne= new ObjOne($data);
       $doObjOne->write();
    }
    for($i=0;$i<10;$i++){
       $doObjTwo= new ObjOne($data);
       $doObjTwo->write();
    }

    Can someone help with a way of doing this without having to go "long hand" and specify everything field and then set each field at the end manually?

    Barry

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Multiple DataObjects (of the same type) on the same form... Link to this post

    Ok, solved, thanks Martijn for the idea...

       function TestForm()
       {
          $fields = new FieldSet();

          for($i=0;$i<5;$i++){
             $fieldsa = singleton('ObjOne')->getFrontendFields();

             foreach ($fieldsa as $f){
                $name = $f->Name();
                $f->setName("ObjOne_{$i}_{$name}");
                $fields->push($f);
             }
          }

          for($i=0;$i<10;$i++){
             $fieldsb = singleton('ObjTwo')->getFrontendFields();
             foreach ($fieldsb as $f){
                $name = $f->Name();

                $f->setName("ObjTwo_{$i}_{$name}");
                $fields->push($f);
             }
          }

          $actions = new FieldSet(new FormAction('DoTestForm', 'go'));

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

       function DoTestForm($data,$form)
       {
          $fieldsa = singleton('ObjOne')->getFrontendFields();
          $fieldsb = singleton('ObjTwo')->getFrontendFields();

          for($i=0;$i<5;$i++){
             $doObjOne= new ObjOne();
             foreach ($fieldsa as $f){
                $name = $f->Name();
                $key = "ObjOne_{$i}_$name";

                $doObjOne->$name = $data[$key];
             }
             $doObjOne->write();
          }
          for($i=0;$i<10;$i++){
             $doObjTwo = new ObjTwo();
             foreach ($fieldsb as $f){
                $name = $f->Name();
                $key = "ObjTwo_{$i}_$name";

                $doObjTwo->$name = $data[$key];
             }
             $doObjTwo->write();
          }

          return Director::redirect(Director::baseURL(). $this->URLSegment.'?posted=1');
       }

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