2016-11-13 1 views
2

我在理解工作流如何在表中插入一些數據時遇到問題。所以我有一個簡單的聯繫形式:如何在Symfony 3問題的數據庫中插入數據

這是我的形式:

{{ 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) }} 

,這是我的控制器的功能:

/** 
     * @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(); 
/// what should I do next here ? 
      }else { 
       return $this->render('contact/content.html.twig', array(
        'form' => $form->createView(), 
       )); 
      } 
     } 

我得到了所有的post請求,但我應該怎麼下一步,你能舉個例子嗎?我如何在Symfony中編寫插入查詢?

+0

它依賴。 '聯繫'是一個學說實體嗎? (例如:http://symfony.com/doc/current/doctrine/registration_form.html#handling-the-form-submission) – Federkun

+0

我在\ AppBundle \ Entity \ Contact.php中有一個Contact類,我不是真的確定什麼是教義,如何使用它? – Chester

+0

另外在哪裏聲明在哪個表中數據將被插入到你的例子中? – Chester

回答

1

通常,處理這種情況最喜歡的方式是創建一個新實體(存儲在AppBundle/Entity目錄中 - 據我所知,您已經創建了它),並且基於該實體,您需要創建一個新的實體表格(存儲在AppBundle/Form目錄中)。

現在,表單的問題是您可以通過多種方式創建它們。其中一種方法就是我已經告訴過你的方法。另一種方法是在控制器方法中創建它,就像你一樣。

總結,以下僅僅是一個例子,使用第一種方式,並使用symfony的> = 2.8:

//1. Create an Entity class, using a symfony console command (after completing all steps, you'll end with the directory AppBundle/Entity and inside it a new php file Contact.php): 
$ php app/console doctrine:generate:entity //and follow the interactive steps, and let's say you need the following columns: name (varchar:255), email (varchar:255), website (varchar:255), and comment (varchar:255). 

//2. Create a new Form, based on that Entity class (after completing all steps, you'll end with the directory AppBundle/Form and inside it a new php file ContactType.php): 
$ php app/console doctrine:generate:form AppBundle:Contact 

//3. In the 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 the above line with 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 the contact object 
     $em->flush();//save it to the 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) }} 

但請注意,因爲如果你使用的是symfony的版本< 2.8,然後你應該看看here來看看如何渲染文本類型(你需要texttype,emailtype和textareatype ---或者你可以讓它們在生成時就足夠了),但是如果你使用symfony> = 2.8 ,那麼您所需要的就是在您的課程頂部導入您正在使用的每種類型的相應課程:

use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\Extension\Core\Type\TextareaType; 
use Symfony\Component\Form\Extension\Core\Type\EmailType; 

而且,建築形式時:

//... 
$builder 
    ->add('name',TextType::class) 
    ->add('email',EmailType::class) 
    ->add('website',TextType::class) 
    ->add('comment',TextareaType::class) 
;