我有两个相关的实体:Magazine和Issue与OneToMany关系。如何创建表单以添加与特定杂志相关的新Issue实体?
当我在控制器中创建表单时,从表单类中调用它以添加问题,如何为它的某些字段传递预先设置的值?
我知道这一定很容易,但是我被困在这里。
您有多种选择:
您可以在其中添加一个隐藏字段,FormType
然后传递Magazine ID
类似的内容。您可以以本教程为例。
详细地,它应该在您的中看起来像这样FormType
:
$builder->add('magazine', 'entity' /*, more options... */);
并在您的Controller
:
$magazine = // get Your magazine here...
$issue = new Issue();
$issue->setMagazine($magazine);
$form = $this->createForm(new YourFormType(), $issue);
您可以简单地生成如下路线:/magazines/{magazine_id}/issues/add
这样可以Magazine
在Controller Action中为您提供实体,如下所示:
public function addAction($magazineId)
{
$magazine = // get by $magazineId
// generate your issue form and stuff
if ($form->isValid()) {
$issue->setMagazine($magazine);
}
}
您可以从那里开始工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句