2016-11-13 11 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(), 
       )); 
      } 
     } 

私はポスト要求のすべてを得たが、何が、私がすべき次に、あなたに私に例を教えてもらえますか? symfonyに挿入クエリを書くにはどうすればいいですか?

+0

それが依存しています。 Doctrineエンティティに「連絡」していますか? (例:http://symfony.com/doc/current/doctrine/registration_form.html#handling-the-form-submission) – Federkun

+0

私は\ AppBundle \ Entity \ Contact.phpにContactクラスを持っています。 Doctrineとはどのようなものですか? – 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 、使用する各タイプのそれぞれのクラスをContactTypeクラスの上部にインポートするだけです:

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) 
; 
関連する問題