1

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

我只是想通过表格更新一些东西。

控制器:

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"}

但我看不到任何更新查询或其他东西的痕迹。如果你有一个想法,那就太好了!

非常感谢,

4

1 回答 1

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
于 2013-08-07T14:48:01.827 回答