1
下一个就是我想要做的是:插入数据到数据库
与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)'?你必须清理你的思想,重新组织你的想法。 – 2013-04-05 14:22:03