symfony doctrine从表单更新

时间:2013-08-07 14:24:37

标签: symfony doctrine-orm

我从今天早上起就被实体更新了。 不知道我错过了什么,很确定这是一个新手的错误。

我只是想通过表单更新内容。

控制器:

public function editAction($pid, $plid, Request $request)
{
    $plan = new Plan();
    $form = $this->createForm(new PlanType(), $plan);

    $plan = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Plan')->findOneByPlid($plid);
    $project = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Project')->findOneByPid($pid);

    $form->handleRequest($request);
    if ($request->getMethod() == 'POST') {

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

            return $this->redirect($this->generateUrl('qarth_framework_plan_edit', array('pid' => $pid, 'plid' => $plid)));
    }

    return $this->render('QArthFrameworkBundle:Pages:plan_edit.html.twig', array(
        'plan' => $plan,
        'project' => $project,
        'form' => $form->createView(),
    ));
}

表格:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name');
    $builder->add('description', 'textarea');
}

实体:http://pastebin.com/bTqKehyQ

使用分析器,我可以看到我的帖子参数发布得很好

plan    {"name":"fsggsfgsf","description":"gsfgsfgsf","_token":"7d089aca0203c60fe1e617488e532ac966101440"}

但我看不到任何更新查询或其他内容的痕迹。 如果你有一个想法,它会很棒!

非常感谢,

1 个答案:

答案 0 :(得分:0)

需要将查询的计划传递给表单。

public function editAction($pid, $plid, Request $request)
{
    $plan = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Plan')->findOneByPlid($plid);
    $project = $this->getDoctrine()->getRepository('QArthFrameworkBundle:Project')->findOneByPid($pid);

// Create a new one if not found
if (!$plan) $plan = new Plan();

// Build your form using queried or new plan
$form = $this->createForm(new PlanType(), $plan);

$form->handleRequest($request);

// Checks for POST as well as validity
if ($form->isValid()) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($plan);  // To handle new plans, no impact for existting plans
        $em->flush();

// Rest is the same