symfony 3.4 form insert to database error500

symfony 3.4 form insert to database error500

I have problem with symfony 3.4… it should pretty simple but… The thing is that when i trying to create simple form and insert data to mysql database AFTER SUBMIT i get error500. Project is already at LIVE server, not localy, so i can`t automatically generate route to function with composer, i wrote it manually. Form renders fine. Mysql fields is id- int(11) AI and title- varchar(255).

My Form class:


use AppBundleEntityIndexAdvantage;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

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' => 'AppBundleEntityIndexAdvantage'
        ));
    }

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

My Entity: AppBundleEntityIndexAdvantage.php

namespace AppBundleEntity;

use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;

/**
 * @ORMTable(name="index_advantage")
 * @ORMEntity(repositoryClass="AppBundleRepositoryIndexAdvantageRepository")
 */
class IndexAdvantage
{
    /**
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

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

My controller AppBundleControllerAdminIndexController.php

namespace AppBundleControllerAdmin;

use AppBundleControllerAbstractBaseController;
use AppBundleFormIndexAdvantageType;
use AppBundleEntityIndexAdvantage;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use DoctrineORMEntityManagerInterface;


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('AppBundleFormIndexAdvantageType', $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()]
        );
    }
}

My form twig AppBundleResourcesviewAdminIndexAdvantagenew.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 %}

I also manually created route to function newAdvantageAction at var/cache/prod/appProdProjectContainerUrlGenerator.php and var/cache/prod/appProdProjectContainerUrlMatcher.php

        'admin.index_advantage.new' => array (0 => array (0 => 'id',),
  1 => array ('_controller' =>'AppBundleControllerAdminAdminIndexController::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 (  ),),

and

                // 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' => 'AppBundleControllerAdminAdminIndexController::newAdvantageAction',));
                    if (!in_array($canonicalMethod, ['GET', 'POST'])) {
                        $allow = array_merge($allow, ['GET', 'POST']);
                        goto not_adminindex_advantagenew;
                    }

                    return $ret;
                }
                not_adminindex_advantagenew:

Source: Symfony Questions

Leave a Reply

Your email address will not be published. Required fields are marked *