symfony - Insert data from form to database -
what want next:
create simple form formbuilder
when form submitted result saved database particular user ( based on id )
in addition code controller:
public function helloaction(request $request, $id){//displaying individual results particular user// // find username in view// $em = $this->getdoctrine()->getmanager(); $query = $em->createquery('select b acmewebbundle:baza b 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 action, such saving task database $em->flush(); return new response('<h1>thanks feedback !!!!</h1>'); } } return $this->render('acmewebbundle:default:hello.html.twig',array('all'=>$total,'id'=>$id ,'form'=>$form->createview())); } }
but creates new row in database, , add value rating column.moreover, id field,username , etc.. empty.
what want is, rating added colum rating, specific id.
in following example create form, data on post
, persist fresh object or modified one, there no point in persisting empty object.
public function historialaction(request $request) { $form = $this->createformbuilder() ->add('phone', 'text', array('attr' => array('autofocus' => ''))) ->add('period', 'number', array('attr' => array('value' => '12'))) ->getform(); if ($request->ismethod('post')) { $form->bind($request); // data array "phone" , "period" keys $data = $form->getdata(); $em = $this->getdoctrine()->getmanager(); $contract = $em->getrepository('frontbundle:contract')->getcontractbyphone($data["phone"]); $contract->setowner("john doe"); $contract->setphone($data["phone"]); // or $contract = new contract("john doe", $data["phone"], $data["period"]); $em->persist($contract); // set/modify properties persist }
Comments
Post a Comment