简体   繁体   English

我无法在数据库Symfony 2.4和准则中插入数据

[英]I can't insert data in my database symfony 2.4 and doctrine

when i check my database i can't find any row , no data found im using symfony 2.4 and doctrine2 I dont know where is the error 当我检查我的数据库时,我找不到任何行,使用symfony 2.4和doctrine2我找不到任何数据,我不知道错误在哪里

Here is my locationType 这是我的locationType

<?php

namespace carRental\MainBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class LocationType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('fullName', 'text', array(
                'attr' => array(
                    'class' => 'form-control'
                )
            ))
            ->add('email', 'email', array(
                'attr' => array(
                    'class' => 'form-control'
        )))
            ->add('phone', 'number', array(
                'attr' => array(
                    'class' => 'form-control'
        )))
            ->add('placeLivrai', 'text', array(
                'attr' => array(
                    'class' => 'form-control'
        )))
            ->add('placeRecu', 'text', array(
                'attr' => array(
                    'class' => 'form-control'
        )))
            ->add('dateLivrai', 'date')
            ->add('dateBack', 'date')
            ->add('car', 'choice', array(
                'choices' => array(
                    'fiatPanda' => 'fiat Panda',
                    'hyundaih1' => 'Hyundai H1',
                    'daciaLogan' => 'Dacia Logan',
                    'fiatpunto' => 'Fiat Punto',
                ),
                'attr' => array(
                    'class' => 'form-control'
                )
            ))
            ->add('country', 'country', array(
                'attr' => array(
                    'class' => 'form-control'
        )))
            ->add('gPS', 'checkbox', array(
                'required' => false
            ))
            ->add('siegeBebe', 'checkbox', array(
                'required' => false
            ))
            ->add('message', 'textarea', array(
                'attr' => array(
                    'class' => 'form-control'
        )))
            ->getForm();
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'carRental\MainBundle\Entity\Location'
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'carrental_mainbundle_location';
}

} }

And this is my locationAction 这是我的位置动作

 public function locationAction() {
    // Reservation Form
    $em = $this->getDoctrine()->getManager();
    $location = new Location();
    $form = $this->createForm(new LocationType(), $location);
    $request = $this->getRequest();
    if ($request->isMethod('POST')) {
        $form->handleRequest($request);
        if ($form->isValid()) {
            $em->persist($form->getData());
            $em->flush();
             $this->get('session')->getFlashBag()->add('Reservation', 'Votre réservation a été bien envoyé');
             /* return $this->redirect($this->generateUrl('home_main_homepage')); */
        }
    }
    return $this->render('carRentalMainBundle:Main:location.html.twig', array(
                'form' => $form->createView(),
    ));
}

This is my view 这是我的看法

    {% extends 'carRentalMainBundle::layout.html.twig' %}
{% block body %}
<div class="container">
{% for flashMessage in app.session.flashbag.get('Reservation') %}
    <ul><li class="alert-success">
    {{flashMessage}}
        </li></ul>
{% endfor%}
    <br/>
    <div class="col-md-4">
        <form  method="POST" action="{{path('carrental_mainbundle_location')}}" {{ form_enctype(form) }} >

         {{form_errors(form)}}
            <div class="form-group">
         {{ form_label(form.fullName,'*Nom et Prénom') }}
        {{ form_errors(form.fullName) }}
        {{ form_widget(form.fullName) }}
            </div>
            <div class="form-group">
            {{ form_label(form.email,'*Email :') }}
        {{ form_errors(form.email) }}
        {{ form_widget(form.email) }}
            </div>
            <div class="form-group">
            {{ form_label(form.phone,'*Téléphone :') }}
        {{ form_errors(form.phone) }}
        {{ form_widget(form.phone) }}

            </div>
            <div class="form-group">

            {{ form_label(form.placeLivrai,'*Place de livraison :') }}
        {{ form_errors(form.placeLivrai) }}
        {{ form_widget(form.placeLivrai) }}
            </div>
            <div class="form-group">
            {{ form_label(form.placeRecu,'*Place de récupération :') }}
        {{ form_errors(form.placeRecu) }}
        {{ form_widget(form.placeRecu) }}

            </div>
            <div class="form-group">

            {{ form_label(form.dateLivrai,'*Date de livraison : ') }}
        {{ form_errors(form.dateLivrai) }}
        {{ form_widget(form.dateLivrai) }}
            </div>
            <div class="form-group">

            {{ form_label(form.dateBack,'*Date de retour :') }}
        {{ form_errors(form.dateBack) }}
        {{ form_widget(form.dateBack) }}
            </div>
            <div class="form-group">

            {{ form_label(form.car,'*Voiture :') }}
        {{ form_errors(form.car) }}
        {{ form_widget(form.car) }}
            </div>
            <div class="form-group">

            {{ form_label(form.country,'*Pays :') }}
        {{ form_errors(form.country) }}
        {{ form_widget(form.country) }}
            </div>
            <div class="form-group">

            {{ form_label(form.gPS,'GPS :') }}
        {{ form_errors(form.gPS) }}
        {{ form_widget(form.gPS) }}
            </div>
            <div class="form-group">

            {{ form_label(form.siegeBebe,'Siége bébé :') }}
        {{ form_errors(form.siegeBebe) }}
        {{ form_widget(form.siegeBebe) }}
            </div>
            <div class="form-group">

            {{ form_label(form.message,'Message :') }}
        {{ form_errors(form.message) }}
        {{ form_widget(form.message) }}
            </div>
            {{form_rest(form)}}
            <input type="submit" value="Envoyer" class="btn btn-primary ">

        </form>
    </div>
</div>
    {% endblock %}

Thank you for your help 谢谢您的帮助

Try to save $location object instead of $form->getData() in your controller. 尝试在控制器中保存$location对象而不是$form->getData() Ie change 即改变

$em->persist($form->getData()); $ EM->坚持($形式 - >的getData());

to

$em->persist($location); $ EM->坚持($位置);

Is carrental_mainbundle_location in form action attribute correct route name of locationAction method? 表单操作属性中的carrental_mainbundle_location是否为locationAction方法的正确路由名称?

It seems you don't use any file fields. 看来您没有使用任何file字段。 So, do you really need {{ form_enctype(form) }} to be included in form tag? 那么,您是否真的需要{{ form_enctype(form) }}包含在表单标记中?

I suggest you to change open and close form tags in your template to {{ form_start(form) }} and {{ form_end(form) }} respectivelly. 建议您将模板中的打开和关闭表单标签分别更改为{{ form_start(form) }}{{ form_end(form) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM