Symfony3仅将占位符值保存到数据库

时间:2017-03-14 12:36:53

标签: php mysql symfony doctrine twig

我试图将实体值保存到MySQL,但即使我更改了值,它们也总是保存为占位符数据。如果我删除占位符数据,则值始终保存为NULL。

这是我的实体类:

<?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('ok@hotmail.com');
        $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()));
    }

1 个答案:

答案 0 :(得分: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('ok@hotmail.com');
        $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()));
    }