1

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

这是我的位置类型

<?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';
}

}

这是我的位置动作

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

谢谢您的帮助

4

1 回答 1

0

Try to save $location object instead of $form->getData() in your controller. I.e. change

$em->persist($form->getData());

to

$em->persist($location);

Is carrental_mainbundle_location in form action attribute correct route name of locationAction method?

It seems you don't use any file fields. So, do you really need {{ form_enctype(form) }} to be included in form tag?

I suggest you to change open and close form tags in your template to {{ form_start(form) }} and {{ form_end(form) }} respectivelly.

于 2014-04-20T09:04:29.897 回答