php - How to insert data in the database in Symfony 3 issue -
i having problems understanding workflow how insert data in table. have simple contact form:
this form:
{{ form_start(form, { 'attr': {'id': 'contact-form'}, 'action': path('contact'), 'method': 'post'} ) }} <div class="text-fields"> <div class="float-input"> <input name="name" id="name" placeholder="name" type="text"> <span><i class="fa fa-user"></i></span> </div> <div class="float-input"> <input name="mail" id="mail" placeholder="e-mail" type="text"> <span><i class="fa fa-envelope-o"></i></span> </div> <div class="float-input"> <input name="website" id="website" placeholder="website" type="text"> <span><i class="fa fa-link"></i></span> </div> </div> <div class="comment-area"> <textarea name="comment" id="comment" placeholder="message"></textarea> </div> <div class="submit-area"> <input type="submit" value="send"/> </div> <div id="msg" class="message"></div> {{ form_end(form) }}
and controller's function:
/** * @route("/contact/", name="contact"). */ public function indexaction() { $contact = new contact(); $form = $this->createformbuilder($contact) ->setaction($this->generateurl('contact')) ->getform(); $request = request::createfromglobals(); if ($request->ismethod('post')) { $params = $request->request->all(); var_dump($params); exit(); /// should next here ? }else { return $this->render('contact/content.html.twig', array( 'form' => $form->createview(), )); } }
i got of post request, should next, can give me example ? how can write insert query in symfony ?
usually, favorite way deal such cases create new entity (stored in appbundle/entity
directory - can understand, created it), , based on entity, need create new form (stored in appbundle/form
directory).
now, problem forms can create them in several ways. 1 way 1 i've told you; way create in controller method, did.
to summarize, following example, using first way, , using symfony >= 2.8:
//1. create entity class, using symfony console command (after completing steps, you'll end directory appbundle/entity , inside new php file contact.php): $ php app/console doctrine:generate:entity //and follow interactive steps, , let's need following columns: name (varchar:255), email (varchar:255), website (varchar:255), , comment (varchar:255). //2. create new form, based on entity class (after completing steps, you'll end directory appbundle/form , inside new php file contacttype.php): $ php app/console doctrine:generate:form appbundle:contact //3. in controller method: use appbundle\entity\contact; use appbundle\form\contacttype; //... /** * @route("/contact/", name="contact") * @method({"get","post"}) */ public function contactaction(request $request){ $contact = new contact(); //for symfony >= 2.8 $form = $this->createform(contacttype::class, $contact, [ //or if you're using symfony < 2.8, replace above line this: $form = $this->createform(contacttype, $contact, [ 'action'=>$this->generateurl('contact'), 'method'=>'post' ]); $form->handlerequest($request); if($form->issubmitted() && $form->isvalid()){ //...more stuff pre-insertion here if needed $em = $this->getdoctrine()->getmanager(); $em->persist($contact);//persist contact object $em->flush();//save db //...more stuff post-insertion here if needed return $this->redirecttoroute('homepage'); } return $this->render('contact/content.html.twig', array( 'form' => $form->createview(), )); } //4. in contact/contact.html.twig: {{ form_start(form) }} <div class="text-fields"> <div class="float-input"> {{ form_row(form.name,{ attr:{ name:'name',id:'name',placeholder:'name' } }) }} <span><i class="fa fa-user"></i></span> </div> <div class="float-input"> {{ form_row(form.email,{ attr:{ name:'email',id:'email',placeholder:'e-mail' } }) }} <span><i class="fa fa-envelope-o"></i></span> </div> <div class="float-input"> {{ form_row(form.website,{ attr:{ name:'website',id:'website',placeholder:'website' } }) }} <span><i class="fa fa-link"></i></span> </div> </div> <div class="comment-area"> {{ form_row(form.comment,{ attr:{ name:'comment',id:'comment',placeholder:'message' } }) }} </div> <div class="submit-area"> <input type="submit" value="send"/> </div> <div id="msg" class="message"></div> {{ form_end(form) }}
but please pay attention, if using symfony version < 2.8, should here see how render text types (you'll need texttype, emailtype, , textareatype --- or can let them generated, enough), if you're using symfony >= 2.8, need import, @ top of contacttype
class, respective classes each type you're using:
use symfony\component\form\extension\core\type\texttype; use symfony\component\form\extension\core\type\textareatype; use symfony\component\form\extension\core\type\emailtype;
and, when building form:
//... $builder ->add('name',texttype::class) ->add('email',emailtype::class) ->add('website',texttype::class) ->add('comment',textareatype::class) ;
Comments
Post a Comment