Symfony 2实体字段类型未选择编辑操作中的现有数据

时间:2015-08-12 13:46:21

标签: php symfony

我有一个实体字段类型我正在使用'编辑'形式。

字段类型列出了正确的选项并且数据仍然存在,但它会选择' top'默认情况下会生成列表,而不是数据库中的数据。

例如,如果我有shelf标记为SH6的记录,并且我要进行编辑,那么在实体字段类型中选择的默认shelf将是列表的顶部,即SH1

这意味着用户可能会编辑unitsInStock并意外更改shelf值,因为他们没有意识到它被设置为错误的内容。更令人讨厌的是,即使你知道这个问题,你也可能不记得应该设置的值。

这是我的控制器动作。

public function editAction($id, Request $request) {

    $em = $this->getDoctrine()->getManager();
    $article20000stock = $em->getRepository('RegenerysQMSBundle:Article20000Stock')->find($id);
    if (!$article20000stock) {
      throw $this->createNotFoundException(
              'No id ' . $id
      );
    }

     $form = $this->createFormBuilder($article20000stock)
        ->add('article20000Information')
        ->add('unitsInStock')
        ->add('expiryDate')
        ->add('shelf', 'entity', array('class' => 'RegenerysQMSBundle:Shelf', 'property' => 'id', ))
        ->add('submit', 'submit')
        ->getForm();

    $form->handleRequest($request);
    if ($form->isValid()) {
       $em->flush();
     return $this->redirectToRoute('regenerys_qms_article20000stock_individual', array('id' => $id));
}

    $build['form'] = $form->createView();
    return $this->render('forms/editArticle20000Stock.html.twig', $build);
 }

1 个答案:

答案 0 :(得分:0)

所以实体缺乏关系,这就是造成这个问题的原因。