无法使用 Symfony2 中的表单将数据插入数据库

发布于 2024-12-06 05:55:20 字数 1767 浏览 0 评论 0原文

我有一个用户输入数据的表单。然后,当他们单击“提交”时,该数据将保存到数据库中。

唯一的事情是,它不这样做。实际发生的情况是,当单击“提交”时,页面会重新加载,并且表单中输入的所有数据仍将显示。我去查数据库,记录还没有更新。

没有错误,但根据探查器,加载页面时会运行三个 SQL 语句。所有这三个语句都是 SELECT 语句,没有一个 INSERT 语句。

这是控制器中页面的代码(包括“INSERT”语句):

    public function addAction(Request $request)
{   

    $pageadd = new Content();
    $form = $this->createForm(new PageAdd(), $pageadd);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();

            $em->persist($pageadd);
            $em->flush();

            return new Response('Created page id '.$pageadd->getId());

        }
    }

    return $this->render('ShoutAdminBundle:Default:pageadd.html.twig', array(
        'form' => $form->createView()
    ));

}

这是表单的代码(由于空间原因我省略了一些字段。但它们都是相同的):

<form action="{{ path('ShoutAdminBundle_adminpageaddpost') }}" method="post" {{ form_enctype(form) }} class="blogger">

    <p class="row">
        {{ form_label(form.id, 'ID*', { 'attr': {'class': 'title'} }) }}
        {{ form_errors(form.id) }}
        {{ form_widget(form.id, { 'attr': {'class': 'textfield'}}) }}
    </p>
    <p class="row">
        {{ form_label(form.title, 'Title*', { 'attr': {'class': 'title'} }) }}
        {{ form_errors(form.title) }}
        {{ form_widget(form.title, { 'attr': {'class': 'textfield'}}) }}
    </p>

    <p class="row">
        <input type="submit" value="Save This Page" class="savebutton" />
    </p>
</form>

如果您需要任何我将向他们提供更多代码。我认为这两段代码就是问题所在。

干杯!

I have a form where the user enters data. Then, when they click submit, this data will be saved to the database.

The only thing is, it doesn't do this. What does happen, is that when submit is clicked, the page reloads and all the data entered in the form will still be displayed. I go to check the database, and the records haven't been updated.

There are no errors, but according to the profiler there are three SQL statements run when the page is loaded. All three of these are SELECT statements, not one INSERT statement there.

Here is the code for the page in the controller (including the "INSERT" statement):

    public function addAction(Request $request)
{   

    $pageadd = new Content();
    $form = $this->createForm(new PageAdd(), $pageadd);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();

            $em->persist($pageadd);
            $em->flush();

            return new Response('Created page id '.$pageadd->getId());

        }
    }

    return $this->render('ShoutAdminBundle:Default:pageadd.html.twig', array(
        'form' => $form->createView()
    ));

}

Here is the code for the form (I've omitted some of the fields for space reasons. But they are all identical):

<form action="{{ path('ShoutAdminBundle_adminpageaddpost') }}" method="post" {{ form_enctype(form) }} class="blogger">

    <p class="row">
        {{ form_label(form.id, 'ID*', { 'attr': {'class': 'title'} }) }}
        {{ form_errors(form.id) }}
        {{ form_widget(form.id, { 'attr': {'class': 'textfield'}}) }}
    </p>
    <p class="row">
        {{ form_label(form.title, 'Title*', { 'attr': {'class': 'title'} }) }}
        {{ form_errors(form.title) }}
        {{ form_widget(form.title, { 'attr': {'class': 'textfield'}}) }}
    </p>

    <p class="row">
        <input type="submit" value="Save This Page" class="savebutton" />
    </p>
</form>

If you need any more code I will provide them. I think these two bits of code is where the problem lies though.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

臻嫒无言 2024-12-13 05:55:20

你必须在持久化之前填充实体,我给你举个例子:

public function saveAction(Request $request)
{
    if ($request->getMethod() == 'POST') {
        // EntityManager
        $em = $this->getDoctrine()->getEntityManager();

        // New entity
        $registration = new Customer();

        // Build the form
        $form = $this->createFormBuilder()
        ->add('name', 'text')
        ->add('country', 'text')
        ->add('email', 'email')
        ->add('certificate', 'text')
        ->add('provider', 'entity', array(
              'class' => 'YourCustomBundle:Partner',
        ))
        ->getForm();

        // Populate
        $form->bindRequest($request);

        // Check
        if($form->isValid()) {
            // Fill the entity
            $registration->setName($form['name']->getData());
            $registration->setCountry($form['country']->getData());
            $registration->setEmail($form['email']->getData());
            $registration->setCertificate($form['certificate']->getData());
            $registration->setProvider($form['provider']->getData());
            $em->persist($registration);
            $em->flush();
        }   

    }
    return $this->render('YourCustomBundle:Default:index.html.twig',array(
            'form' => $form->createView(),
    ));
}

You must fill the entity before the persist, I give you an example:

public function saveAction(Request $request)
{
    if ($request->getMethod() == 'POST') {
        // EntityManager
        $em = $this->getDoctrine()->getEntityManager();

        // New entity
        $registration = new Customer();

        // Build the form
        $form = $this->createFormBuilder()
        ->add('name', 'text')
        ->add('country', 'text')
        ->add('email', 'email')
        ->add('certificate', 'text')
        ->add('provider', 'entity', array(
              'class' => 'YourCustomBundle:Partner',
        ))
        ->getForm();

        // Populate
        $form->bindRequest($request);

        // Check
        if($form->isValid()) {
            // Fill the entity
            $registration->setName($form['name']->getData());
            $registration->setCountry($form['country']->getData());
            $registration->setEmail($form['email']->getData());
            $registration->setCertificate($form['certificate']->getData());
            $registration->setProvider($form['provider']->getData());
            $em->persist($registration);
            $em->flush();
        }   

    }
    return $this->render('YourCustomBundle:Default:index.html.twig',array(
            'form' => $form->createView(),
    ));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文