2017-03-14 10 views
1

エンティティの値をMySQLに保存しようとしていますが、値を変更しても常にプレースホルダのデータとして保存されます。プレースホルダデータを削除すると、値は常にNULLとして保存されます。symfony3はプレースホルダの値をデータベースに保存するだけです

は、ここに私のエンティティクラスです:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Message 
* 
* @ORM\Table(name="message") 
* @ORM\Entity 
*/ 
class Message 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=50, nullable=true) 
    */ 
    private $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="addy", type="string", length=50, nullable=true) 
    */ 
    private $addy; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="subject", type="string", length=50, nullable=true) 
    */ 
    private $subject; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="body", type="string", length=200, nullable=true) 
    */ 
    private $body; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 



    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Message 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set addy 
    * 
    * @param string $addy 
    * 
    * @return Message 
    */ 
    public function setAddy($addy) 
    { 
     $this->addy = $addy; 

     return $this; 
    } 

    /** 
    * Get addy 
    * 
    * @return string 
    */ 
    public function getAddy() 
    { 
     return $this->addy; 
    } 

    /** 
    * Set subject 
    * 
    * @param string $subject 
    * 
    * @return Message 
    */ 
    public function setSubject($subject) 
    { 
     $this->subject = $subject; 

     return $this; 
    } 

    /** 
    * Get subject 
    * 
    * @return string 
    */ 
    public function getSubject() 
    { 
     return $this->subject; 
    } 

    /** 
    * Set body 
    * 
    * @param string $body 
    * 
    * @return Message 
    */ 
    public function setBody($body) 
    { 
     $this->body = $body; 

     return $this; 
    } 

    /** 
    * Get body 
    * 
    * @return string 
    */ 
    public function getBody() 
    { 
     return $this->body; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
} 

そして、ここに私のコントローラです:

/** 
    * @Route("/index.html.twig", name="new") 
    */ 
    public function newAction(Request $request) 
    { 
     // create a message instance 
     $message = new Message(); 

     //assign some placeholder data 
     $message->setName('Billy'); 
     $message->setAddy('[email protected]'); 
     $message->setSubject('Notice'); 
     $message->setBody('Practice Delayed'); 

     //method creates and renders form 
     $form = $this->createFormBuilder($message) 
      ->add('name', TextType::class,array('label' => 'From')) 
      ->add('addy', TextType::class,array('label' => 'To')) 
      ->add('subject', TextType::class) 
      ->add('body', TextType::class) 
      ->add('save', SubmitType::class, array('label' => 'Send', 'attr' => array('class' => 'btn btn-danger btn-lg'))) 
      ->getForm(); 

     //method checks if the form is submitted 
     $form->handleRequest($request); 

     $message = $form->getData(); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($message); 
     $em->flush(); 

     return $this->render('default/index.html.twig', array('form' => $form->createView())); 
    } 
+0

ます$ form-

を提出していなかった場合でも、あなたのコントローラは、常にデータを保存

> handleRequestは送信されたフォームをチェックしません。 $ form-> isValid()を忘れた。 http://symfony.com/doc/current/forms.html#handling-form-submissions。 newActionがPOSTでのみ呼び出される場合を除きますか? – Cerad

+0

$ form-> isVald()検証を追加すると、データベースに何も保存されません。 – bigmammoo

+0

新しいプロジェクトを作成し、ドキュメントの例を参照してください。この時点で、あなたが何をしているのか、間違っていないのか分かりにくいです。 $ message = $ form-> getData();ラインは不要です。 – Cerad

答えて

0
フォームが
/** 
    * @Route("/index.html.twig", name="new") 
    */ 
    public function newAction(Request $request) 
    { 
     // create a message instance 
     $message = new Message(); 

     //assign some placeholder data 
     $message->setName('Billy'); 
     $message->setAddy('[email protected]'); 
     $message->setSubject('Notice'); 
     $message->setBody('Practice Delayed'); 

     //method creates and renders form 
     $form = $this->createFormBuilder($message) 
      ->add('name', TextType::class,array('label' => 'From')) 
      ->add('addy', TextType::class,array('label' => 'To')) 
      ->add('subject', TextType::class) 
      ->add('body', TextType::class) 
      ->add('save', SubmitType::class, array('label' => 'Send', 'attr' => array('class' => 'btn btn-danger btn-lg'))) 
      ->getForm(); 

     //method checks if the form is submitted 
     $form->handleRequest($request); 
     if ($form->isSubmitted()) { // Add this 
      // I also check if method is POST and if the form is valid ($form->isValid()) but this is not mandatory 
      $message = $form->getData(); 

      $em = $this->getDoctrine()->getManager(); 
      $em->persist($message); 
      $em->flush(); 
     } 
     return $this->render('default/index.html.twig', array('form' => $form->createView())); 
    } 
+0

何も起こっていません。 – bigmammoo

関連する問題