【发布时间】:2017-06-29 15:06:24
【问题描述】:
接下来我要做的是:
使用 FormBuilder 创建简单的表单
提交表单时,将结果保存到特定用户的数据库中(基于其 ID)
另外还有来自控制器的代码:
public function helloAction(Request $request, $id){//displaying individual results for particular user//
// find the username which was in the view//
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT b FROM AcmeWebBundle:baza b WHERE b.id = :id' )
->setParameter('id',$id);
$total = $query->getResult();
$baza = new baza ();
$em = $this->getDoctrine()->getManager();
$em->persist($baza);
$form = $this->createFormBuilder($baza)
->add ('rating','choice',array('label'=>'TEST44','choices'=>array(
'1'=>'1',
'2'=>'2',
'3'=>'3',
'4'=>'4'
),
'expanded'=>true,
'multiple'=>false
))
->getForm();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// perform some action, such as saving the task to the database
$em->flush();
return new Response('<h1>THANKS FOR Your feedback !!!!</h1>');
}
}
return $this->render('AcmeWebBundle:Default:hello.html.twig',array('all'=>$total,'id'=>$id ,'form'=>$form->createView()));
}
}
但这会在数据库中创建新行,并且仅为评分列添加值。此外,id字段,用户名等都是空的。
我想要做的是,为列评级添加评级,但针对特定 ID。
【问题讨论】:
-
您是否在每次“GET”请求时都致电
$em->persist($baza)?你必须清理你的头脑,重新组织你的想法。
标签: symfony symfony-2.1 symfony-forms