【问题标题】:Edit entity part with form用表单编辑实体部分
【发布时间】:2019-08-24 19:03:09
【问题描述】:

我有一个实体(大约 20/25 个字段),我想用表单对其进行编辑。

我只想编辑(和显示)几个表单域。

问题是,所有显示的字段都正确更新,但未渲染的字段默认更新为“null”值。

我的控制器:

$em = $this->getDoctrine()->getManager();

$LaboRequest= $em->getRepository('MyBundle:LaboRequest')->find($id);
$form = $this->createForm('MyBundle\Form\LaboRequestType', $LaboRequest);

if ($request->isMethod('POST') && $form->handleRequest($request)->isSubmitted() && $form->isValid()) {

    $em->persist($LaboRequest);
    $em->flush();

    return $this->redirectToRoute(...);
}

return $this->render('...', array(
            'LaboRequest' => $LaboRequest,
            'form' => $form->createView(),
        ));

我只在我的视图中渲染几个字段,所以我可以理解,默认情况下,symfony 对不渲染的字段使用“null”。

但是有没有办法编辑实体的一部分而不影响具有“null”值的实体的数据?

【问题讨论】:

标签: php forms symfony


【解决方案1】:

我不确定你能做到这一点。 但是你可以扩展你的原始表单并调用

$builder->remove('xxx')

对于您要删除的每个字段

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多