symfony 3.4表单插入数据库错误500

问题描述 投票:-1回答:1

我在使用symfony 3.4时遇到问题...它应该很简单,但是...问题是,当我尝试创建简单的表单并将数据插入到mysql数据库后,提交错误时,我得到error500。项目已经在LIVE服务器上,而不是在本地服务器上,所以我不能自动生成路由以与作曲家一起使用,而是手动编写的。形式很好。 Mysql字段是id- int(11)AI和标题-varchar(255)。

我的表单类:


use AppBundle\Entity\IndexAdvantage;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IndexAdvantageType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, ['label' => 'Title'])
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\IndexAdvantage'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_index_advantage';
    }
}

我的实体: AppBundle \ Entity \ IndexAdvantage.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Table(name="index_advantage")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\IndexAdvantageRepository")
 */
class IndexAdvantage
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param string $title
     * @return $this
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }
}

我的控制器 AppBundle \ Controller \ Admin \ IndexController.php

namespace AppBundle\Controller\Admin;

use AppBundle\Controller\AbstractBaseController;
use AppBundle\Form\IndexAdvantageType;
use AppBundle\Entity\IndexAdvantage;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;


class AdminIndexController extends AbstractBaseController
{
    /**
     * @Route("/admin/")
     */
    public function IndexAction()
    {
        return $this->render('AppBundle:AdminIndex:index.html.twig', array());
    }
...<<<<SOME OTHER FUNCTIONS>>>>>>>.....
    /**
     * @Route(
     *      "/advantage/new",
     *      name="admin.index_advantage.new",
     *      methods={"GET", "POST"}
     * )
     */
    public function newAdvantageAction(Request $request)
    {   
        $advantage = new IndexAdvantage();
        $form = $this->createForm('AppBundle\Form\IndexAdvantageType', $advantage);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $advantage = $form->getData();
            $em = $this->getDoctrine()->getManager();
            $em->persist($advantage);
            $em->flush($advantage);
        }

        return $this->render(
            'AppBundle:AdminIndexAdvantage:new.html.twig',['form' => $form->createView()]
        );
    }
}

我的表单树枝 AppBundle \ Resources \ view \ AdminIndexAdvantage \ new.html.twig

{% extends 'adminLayout.html.twig' %}

{% block content %}
    <h1>NEW</h1>
    {{ form_start(form) }}
        {{ form_widget(form) }}
        <input class="btn btn-success" type="submit" value="Create" />
    {{ form_end(form) }}
{% endblock %}

我还在newAdvantageActionvar/cache/prod/appProdProjectContainerUrlGenerator.php处手动创建了功能var/cache/prod/appProdProjectContainerUrlMatcher.php的路由>

        'admin.index_advantage.new' => array (0 => array (0 => 'id',),
  1 => array ('_controller' =>'AppBundle\\Controller\\Admin\\AdminIndexController::newAdvantageAction',),
  2 => array (  ),
  3 => array (0 => array (0 => 'text', 1 => '/advantage/new',), 1 =>array (0 => 'variable', 1 => '/', 2 => '[^/]++', 3 => 'id',), 2 => array ( 0 => 'text', 1 => '/admin/index',),), 4 => array (  ), 5 => array (  ),),

                // admin_index_advantage_new
                if (0 === strpos($pathinfo, '/admin/index') && preg_match('#^/admin/index/(?P<id>[^/]++)/advantage/new$#sD', $pathinfo, $matches)) {
                    $ret = $this->mergeDefaults(array_replace($matches, ['_route' => 'admin.index_advantage.new']), array (  '_controller' => 'AppBundle\\Controller\\Admin\\AdminIndexController::newAdvantageAction',));
                    if (!in_array($canonicalMethod, ['GET', 'POST'])) {
                        $allow = array_merge($allow, ['GET', 'POST']);
                        goto not_adminindex_advantagenew;
                    }

                    return $ret;
                }
                not_adminindex_advantagenew:

我在使用symfony 3.4时遇到问题...它应该很简单,但是...问题是,当我尝试创建简单的表单并将数据插入mysql数据库后,提交后我得到error500。项目是...

forms symfony submit
1个回答
0
投票

我添加了路线admin_index_advantage_new:

© www.soinside.com 2019 - 2024. All rights reserved.