Silverstripe 通过控制器返回填充的表单

Silverstripe returning a populated form via controller

我正在尝试找出 return 返回一个预先填充了数据库数据的表单的正确方法。让我告诉你我现在是怎么做的:

团队页面:

<?php
class TeamsPage extends Page {
  private static $has_many = array (
    'Teams' => 'Team',
  );
  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Teams', GridField::create(
        'Teams',
        'Teams on this page',
        $this->Teams(),
        GridFieldConfig_RecordEditor::create()
    ));
    return $fields;
  }
}
class TeamsPage_Controller extends Page_Controller {
  private static $allowed_actions = array (
    'show', 'edit', 'EditTeamForm'
  );

  public function EditTeamForm($teamId){
    $fields = new FieldList(
        new TextField('TeamName'),
        new TextareaField('TeamDescription')
    );
    $actions = new FieldList(
        new FormAction('EditTeam', 'Save Changes')
    );
    $requiredFields = new RequiredFields(array('TeamName','TeamDescription'));
    $form = new Form($this, 'EditTeamForm', $fields, $actions, $requiredFields);
    $form->setFormMethod('POST', true);

    $data = Session::get("FormData.{$form->getName()}.data");
    $team = Team::get()->byID($teamId);
    return $data ? $form->loadDataFrom($data) : $form->loadDataFrom($team);
  }

  public function show(SS_HTTPRequest $request) {
    $team = Team::get()->byID($request->param('ID'));

    if(!$team) {
        return $this->httpError(404,'That team could not be found');
    }

    return array (
        'Team' => $team
    );
  }

  public function edit(SS_HTTPRequest $request){
    $team = Team::get()->byID($request->param('ID'));

    if(!$team) {
        return $this->httpError(404,'That team could not be found');
    }

    return array (
        'Team' => $team
    );
  }
}

团队:

<?php
class Team extends DataObject {
  private static $db = array(
    'TeamCaptain' => 'Int',
    'TeamName' => 'Varchar',
    'TeamDescription' => 'Text'
  );
  private static $has_one = array (
    'Photo' => 'Image',
    'TeamsPage' => 'TeamsPage'
  );
  private static $summary_fields = array (
    'GridThumbnail' => '',
    'TeamCaptain' => 'Team Captain',
    'TeamName' => 'TeamName',
    'TeamDescription' => 'Team Description',
  );
  public function getGridThumbnail() {
    if($this->Photo()->exists()) {
        return $this->Photo()->SetWidth(100);
    }
    return '(no image)';
  }
  public function getCMSFields() {
    $fields = FieldList::create(
        TextField::create('TeamCaptain'),
        TextField::create('TeamName'),
        TextareaField::create('TeamDescription'),
        $uploader = UploadField::create('Photo')
    );
    $uploader->setFolderName('teams-photos');
    $uploader->getValidator()->setAllowedExtensions(array(
        'png','gif','jpeg','jpg'
    ));
    return $fields;
  }

  public function Link() {
    return $this->TeamsPage()->Link('show/'.$this->ID);
  }
}

TeamsPage_edit.ss

<% if GetMember() %>
  Welcome $getMember.FirstName<br />
  $EditTeamForm($Team.ID)
  <a href="home">Back to Home</a>
<% else %>
  $GoToLogin()
<% end_if %>

正如您在我的控制器中看到的那样,我正在 return 建立一个我不完全了解其工作原理的 $Team。在我的模板中,我使用 $Team.ID 并且我试图弄清楚该值是否来自我 returning 团队数组或其他方式。我想了解的另一件事是,为什么我不在这里 return 一个表单,为什么我必须有一个单独的函数来创建该表单并且必须在模板中这样调用它。我在这里看到的最后一个问题是,我正在两次查询数据库以获取相同的信息。

我最后要问的是,如果有人能告诉我实现我正在做的事情的正确方法是什么。我敢肯定已经做过很多次了,但是我在互联网上的任何地方都找不到有关如何使用编辑表单的示例。我找到了如何使用 show action 和 return 一堆结果,但没有与表格结合使用。

我建议简单地执行以下操作(这些是对您的代码的补充,而不是完整的 class):

class TeamPage_Controller extends Page_Controller {
    protected $currentTeam;

    protected function getCurrentTeam() {
        if (!isset($this->currentTeam)) {
            $teamID = $this->getRequest()->param('ID') ?: $this->getRequest()->postVar('ID');
            $this->currentTeam = $teamID ? Team::get()->byID($teamID) : null;
        }
        return $this->currentTeam;
    }

    public function EditTeamForm() {
         $team = $this->getCurrentTeam();             
         // You'll want to add a HiddenField with the ID if $team is not null
         // everything else is the same
    }

    // NOTE: I would suggest you call this something else
    // SS convention would be "doSaveTeam" - it should be whatever your FormAction is called though
    public function EditTeam($data, $form) {
        $team = $this->getCurrentTeam();
        // Save and redirect
    }

    public function edit(SS_HTTPRequest $request){
        $team = $this->getCurrentTeam();
        // Blah blah blah
    }
}