我无法在我的数据库symfony 2.4和doctrine中插入数据

时间:2014-04-18 17:59:06

标签: php forms doctrine-orm symfony-2.4

当我检查我的数据库时,我找不到任何行,没有找到数据 即时通讯使用symfony 2.4和doctrine2 我不知道错误在哪里

这是我的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';
}

}

这是我的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(),
    ));
}

这是我的观点

    {% 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 %}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试在控制器中保存$location对象而不是$form->getData()。即改变

  

$ EM-&GT;坚持($形式 - &GT;的getData());

  

$ EM-&GT;坚持($位置);

表单操作属性中的carrental_mainbundle_locationlocationAction方法的正确路由名吗?

您似乎不使用任何file字段。那么,您真的需要将{{ form_enctype(form) }}包含在表单标记中吗?

我建议您将模板中的打开和关闭表单标记分别更改为{{ form_start(form) }}{{ form_end(form) }}