php - Symfony主义,无形式更新实体?

标签 php forms symfony doctrine-orm

不确定我是否正确,但我在 Twig 模板中制作了自己的自定义表单,其中操作路径转到将更新实体的 Controller 。我只见过使用带有 $form->handleRequest($request) 的表单,然后使用 $em->flush(); 的更新方法,因为我还没有见过我通过 Symfony 的表单组件创建了一个表单,我不知道如何从模板访问它以便将其刷新到数据库中。

这是我的 Action Controller 的方式:

/**
 * @param $subid
 * @param Request $request
 * @Route("/editparts/{subid}/", name="updateparts")
 * @Template("editparts.html.twig")
 * @Method("POST")
 */
public function updatePartsAction(Request $request, $subid) {
    $r = $this->getDoctrine()->getManager();

    $entity = $r->getRepository('MainBundle:MainSub')->findOneById($subid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Parts entity to edit.');
    }

    // what is this step???

    $r->flush();

.....

我在 Twig 模板中的表单是这样的:

{% if parts is defined %}
     <div class="inventorysearch">
       <form action="{{ path('updateparts', {'subid' : parts.subid}) }}" method="POST" >
            <input type="text" name="part" value="{{ parts.part }}" disabled><br />
            <input type="text" name="batch" value="{{ parts.batch }}" disabled><br />
            <input type="text" name="rack" required="required" value="{{ parts.rack }}"><br />
           <input type="text" name="acode" value="{{ parts.acode }}"><br />
           <input type="text" name="bcode" value="{{ parts.bcode }}"><br />
           <input type="integer" name="qty" required="required" value="{{ parts.qty }}"><br />
            <button type="submit" name="submit">Update</button>
       </form>
     </div>
    {% endif %}

最佳答案

最好和推荐的方法是使用 symfony 的表单生成器

public function updatePartsAction(Request $request, $subid) {
    $r = $this->getDoctrine()->getManager();

    $entity = $r->getRepository('MainBundle:MainSub')->findOneById($subid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Parts entity to edit.');
    }
    $form= $this->createFormBuilder($entity)
           ->add('part')
           ->add('batch')
           .... and so on the properties from your entity that you want them to edit
           ->getForm();
 if ($this->getRequest()->getMethod() == "POST") {
        $form->handleRequest($request)
        if ($form->isValid()) {
        $r->persist($form->getData());
        $r->flush();
        }
  }

}

在 twig 中,只需渲染您的 {{ form(form) }}

不推荐您要求的另一种方式,也不是一个好的做法,但这取决于您将如何以好的方式或坏的方式编码您的应用程序

public function updatePartsAction(Request $request, $subid) {
    $r = $this->getDoctrine()->getManager();

    $entity = $r->getRepository('MainBundle:MainSub')->findOneById($subid);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Parts entity to edit.');
    }
  if ($this->getRequest()->getMethod() == "POST") {
    $entity->setPath('get values from request')
     and others setters to which you want them to edit        
        $r->persist($entity);
        $r->flush();

  }
}

关于php - Symfony主义,无形式更新实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23571284/

相关文章:

php - 使用查询生成器比较日期

php - 在多列中搜索

javascript - 选中复选框时如何将选择值更改为特定值

javascript - 下拉选择表单,转到提交时的 URL

javascript - 表单文本字段只接受数字和空格

Symfony 欢迎页面在全新安装时未加载

php - 更改翻译路径 Symfony 和 DDD

php - Symfony onFlush 学说监听器

php - Windows Azure 中的非拉丁字符 URL 重写

Symfony Assetic 功能似乎不起作用