6

示例:我的成员(可以登录和更新其数据)具有一项或多项资格。所以我有一个具有 has_one/has_many 关系的 DataObject 'Members' 和一个 DataObject 'Qualification'。

像这样的东西:

class Qualification extends DataObject {

    private static $db = array (
        'Title' => 'Text',
        'From' => 'Date',
        'Till' => 'Date'
    );

    private static $has_one = array (
        'Member' => 'Member',
    );

...

class Member extends DataObject {

   ...

   private static $has_many = array (
      'Qualifications' => 'Qualification',
  );

...

现在我想在前端构建一个表单,允许成员一次添加许多资格,并以相同的形式更新现有的资格。

它可能看起来像这样

资格一

标题:xxx(文本字段)从:xxx(日期字段)到:xxx(日期字段)

资格二

标题:xxx(文本字段)从:xxx(日期字段)到:xxx(日期字段)

+ 添加资格

最好的方法是什么?

我可以使用 jQuery 像这样动态添加字段:http: //jsfiddle.net/nzYAW/

但是我该如何处理更新并将它们添加到数据库中。我尝试的一切都非常复杂和凌乱,所以我想也许其他人有一个我目前看不到的想法。每一个帮助将不胜感激!

4

1 回答 1

3

我用 3dgoo 的解决方案解决了我的问题。我在前端表单中使用 GridField 与GridField 扩展模块和组件GridFieldEditableColumns以及GridFieldAddNewInlineButton. 这是一个例子:

public function MyForm() {

     $config = GridFieldConfig::create();
     $config->addComponent(new GridFieldButtonRow('before'));
     $config->addComponent(new GridFieldEditableColumns());
     $config->addComponent(new GridFieldAddNewInlineButton());
     $gridField = GridField::create('Qualifications', 'Qualifications', Qualification::get()->filter(array('MemberID' => Member::currentUserID()))),$config);

     $fields = new FieldList(

          .... here goes some other Fields like Textfields ...

          TextField::create('MyTextField'),
          CheckboxField::create('MyCheckboxField'),
          $gridField,
     );


     $actions = new FieldList(
          FormAction::create('myAction','save'),
          FormAction::create('myOtherAction','save and next')
     );

     $form = new Form($this, __FUNCTION__, $fields, $actions);
     $form->loadDataFrom(Member::get()->byID(Member::currentUserID()));
     return $form;

}

public function myAction($data, $form) {
      $member = Member::get()->byId(Member::currentUserID());
      $form->saveInto($member);
      $member->write();      
}

我还必须将 canView、canEdit、canCreate 和 canDelete 函数添加到 Qualification DataObject 以允许对其进行编辑和显示。

于 2016-01-27T10:02:20.453 回答