Symfony主义,更新实体没有形式


Symfony doctrine, update entity without form?

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

我的动作控制器是这样的:

/**
 * @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();
  }
}

这个概念违背了MVC的整个目的(试图在模板/视图中做到这一点),所以我不确定你的最后一句话是什么意思。

然而,在你的控制器中你应该通过实体方法访问实体(模型)。

。如果我有一个$user实体和一个username属性,我应该有一个setUsername方法,可以做:

 $user->setUsername('theusername');
 $em->persist($user); 
 $em->flush();

注意,我使用$em而不是$r是出于习惯(这是文档中显示的,也是大多数程序员会使用的)。

而且,从长远来看,使用表单组件仍然是一种更好的方法。