Symfony-向数据库添加记录

时间:2018-09-21 09:26:27

标签: database forms symfony add crud

我用一种具有向数据库添加新数据的动作的形式编写了代码。似乎一切都以正确的方式连接,但是“添加”按钮没有响应,到目前为止我还没有收到任何错误。

  

我的控制器

 /**
 * @Route("/theater-add", name="theater_add")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 * @throws \Exception
 */
public function theaterAddAction(Request $request)
{

    $form = $this->createFormBuilder()
        ->add('name', TextType::class, array('label' => 'Theater Name', 'attr' => ['class'=>'form-control']))
        ->add('town', TextType::class, array('label' => 'Town', 'attr' => ['class'=>'form-control']))
        ->add('address', TextType::class, array('label' => 'Address', 'attr' => ['class'=>'form-control']))
        ->add('phone', TextType::class, array('label' => 'Phone', 'attr' => ['class'=>'form-control']))
        ->add('capacity', TextType::class, array('label' => 'Capacity', 'attr' => ['class'=>'form-control']))
        ->add('seats', TextType::class, array('label' => 'Seats', 'attr' => ['class'=>'form-control']))
        ->add('save', SubmitType::class, array('label' => 'Add', 'attr' => ['class' => 'btn btn-primary pull-right action-save']))
        ->getForm();


   $form->handleRequest($request);

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

        $data = $form->getData();
        $name = $data['name'];
        $town = $data['town'];
        $address = $data['address'];
        $phone = $data['phone'];
        $capacity = $data['capacity'];
        $seats = $data['seats'];

        $this->container->get('theater')->addNewTheater($name, $town, $address, $phone, $capacity, $seats);
    }

    $build['form'] = $form->createView();
    return $this->render('@AdminTemplates/pages/theater-add.html.twig', $build);
}
  

服务

public function addNewTheater($name, $town, $address, $phone, $capacity, $seats)
{
    $newTheater = new Theater();
    $newTheater -> setName($name);
    $newTheater -> setTown($town);
    $newTheater -> setAddress($address);
    $newTheater -> setPhone($phone);
    $newTheater -> setCapacity($capacity);
    $newTheater -> setSeats($seats);

    $this->em->persist($newTheater);
    $this->em->flush();

    return $newTheater;
}
  

我的树枝视图

{{ form_start(form, {'action': path('theater_add'), 'method': 'GET'}) }}
                <form>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                {{ form_row(form.name) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                {{ form_row(form.town) }}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                {{ form_row(form.address) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            {{ form_row(form.phone) }}
                        </div>
                        <div class="col-md-4">
                            {{ form_row(form.capacity) }}
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                {{ form_row(form.seats) }}
                            </div>
                        </div>
                    </div>
                </form>
                {{ form_end(form) }}

我需要某种指导,因为我无法在线找到任何解决方案。

2 个答案:

答案 0 :(得分:0)

您的树枝模板中有一个错误:

{{ form_start(form, {'action': path('theater_add'), 'method': 'GET'}) }}
<form>
...
</form>
{{ form_end(form) }}

{{ form_start(form) }}呈现一个表单元素(在HTML模板中),而{{ form_end(form) }}呈现一个</form>结束标记,因此基本上可以创建以下HTML:       

单击“提交”按钮时,将提交内部表单,该表单不处理控制器中的$request对象,因此($form->isSubmitted() && $form->isValid())永远不会解析为true,而后面的if(真正发生添加的地方)永远不会执行。

总结一下: 只需在模板中删除form_start之后和form_end之前。

答案 1 :(得分:0)

这是一个简单的错误。

我两次关闭了表单标签,因此“添加”按钮不在表单中。

</form>
{{ form_end(form) }}