在 ZF2 中将数据绑定到我的表单中

Bind data into my form in ZF2

我有一个在 viewscript 中使用的表单。我的 viewscript 如下所示:

<?php
$this->titel = "Arbeitskalender Termine";
$this->headTitle($this->titel);
foreach($this->aktermine as $termin) :
    $this->nr=$this->escape($termin->nr);
    $this->kopfnr=$this->escape($termin->kopfnr);
    $this->datum=$this->escape($termin->datum);
    $this->zeit=$this->escape($termin->zeit);
    $this->thema=$this->escape($termin->thema);
    echo $this->form ;
endforeach;
?>

我得到了我的表格(它是一个 table)我得到了与我 table 中的记录相同的表格重复。但是我在表单字段中没有看到任何记录。怎么了?如何获取每个字段中数据集对象的值? 如果我在 html 中使用 viewscript,它工作正常。

使用表单的 bind() 方法将您的模型附加到表单。每个字段的值将从模型中提取并显示在表单中。

This is used in two ways:

  • When displaying the form, the initial values for each element are extracted from the model.

  • After successful validation in isValid(), the data from the form is put back into the model.

要使用此方法,您需要在模型中实现 getArrayCopy()exchangeArray() Aktermine

所以在你的行动中你会得到这样的东西:

$form = new YourForm();
$form->bind($aktermine);

请参阅文档中 Editing an Album 的示例。

另读Binding Objects to Forms

如果您使用的是学说,只需将 getArrayCopy() 添加到您的实体中,如下所示:

public function getArrayCopy(){
  return get_object_vars($this);
}

然后在您的控制器操作中:

$form->bind($yourEntity);