php - Symfony3 只将占位符值保存到数据库

标签 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;
    }
}

这是我的 Controller :

 /**
     * @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()));
    }

最佳答案

即使表单未提交,您的 Controller 也会始终保存数据

    /**
     * @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()));
    }

关于php - Symfony3 只将占位符值保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42786218/

相关文章:

php - 遍历 mysql 结果并在解析为 json 之前将函数应用于每一行

php - mysql_connect 从本地计算机到远程服务器

java - Java 中的多个 SQL 语句(使用 ?allowMultiQueries=true)

php - JMSSerializer + 表单/数组

php - 正则表达式的正则表达式?

php - SQL:使用计数查询返回 1

mysql - 查找哪个表有该列数据

c# - sql查询中的可选搜索参数和具有空值的行

php - 在 Symfony 项目上使用 "Class Table Inheritance"加入 Doctrine 太多

php - 如何覆盖 symfony2 中的注销操作