我正在使用 AnnotationForms
,并且我将教程中的标准编辑操作更改为使用 Annotation 而不是标准表单。
除了 $form->bind()
没有填写值外,一切正常。表单字段保持为空。
我检查了应该绑定(bind)的变量,它已设置且看起来不错。
这是我的操作:
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
if (!$id) {
return $this->redirect()->toRoute('album', array('action'=>'add'));
}
$album = $this->getEntityManager()->find('Album\Entity\Album', $id);
$builder = new AnnotationBuilder();
$form = $builder->createForm(new \Album\Entity\Album());
$form->add(new \MyVendor\Form\MyFieldset());
$form->setBindOnValidate(false);
$form->bind($album);
最佳答案
好吧,这很简单!
诀窍是将对象转换为数组并使用 setData()
而不是绑定(bind)。
我找到了解决方案提示 here .
您仍然需要 bind()
来保存更改。如果您将其遗漏,则不会发生错误,但也不会保存它。
$album = $this->getEntityManager()->find('Album\Entity\Album', $id);
...
$form->bind($album);
$form->setData($album->getArrayCopy());
关于php - Zend Framework 2 - 注释形式 - 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019272/