提交到数据库

时间:2016-02-18 17:50:18

标签: symfony

当我验证我在phpMyAdmin的表格中没有任何内容时,我无法将表单提交到数据库。这是控制器中的代码。我想提交一个包含上传类型的表单。

<?php

// src/AppBundle/Controller/ProductController.php
namespace Upload\FileBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Upload\FileBundle\Entity\Product;
use Upload\FileBundle\Form\ProductType;
use Symfony\Component\Form\FormView;


class ProductController extends Controller
{
/**
 * @Route("/product/new", name="app_product_new")
 */
public function newAction(Request $request)
{
    $product = new Product();
    $form = $this->createForm(new ProductType(), $product);
    $form->handleRequest($request);

    if ($form->isValid()) {
        // $file stores the uploaded PDF file
        /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $product->getBrochure();

        // Generate a unique name for the file before saving it
        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        // Move the file to the directory where brochures are stored
        $brochuresDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/brochures';
        $file->move($brochuresDir, $fileName);

        // Update the 'brochure' property to store the PDF file name
        // instead of its contents
        $product->setBrochure($fileName);

        // ... persist the $product variable or any other work

        return $this->redirect($this->generateUrl('app_product_list'));
    }


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

        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->flush();

        return $this->redirect($this->generateUrl('upload_file_success'));
    }



    return $this->render('UploadFileBundle:Product:new.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

2 个答案:

答案 0 :(得分:0)

因为你的逻辑错了。您首先检查表单是否以及将代码重定向到列表页面。但是你的代码永远不会再次if

答案 1 :(得分:0)

你的逻辑错了。

试试这段代码:

<?php

// src/AppBundle/Controller/ProductController.php
namespace Upload\FileBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Upload\FileBundle\Entity\Product;
use Upload\FileBundle\Form\ProductType;
use Symfony\Component\Form\FormView;


class ProductController extends Controller
{
    /**
     * @Route("/product/new", name="app_product_new")
     */
    public function newAction(Request $request)
    {
        $product = new Product();
        $form = $this->createForm(new ProductType(), $product);
        if ($request->isMethod(Request::POST))
        {
            $form->handleRequest($request);
            if ($form->isValid()) {
                // $file stores the uploaded PDF file

                $file = $product->getBrochure();
                /* @var Symfony\Component\HttpFoundation\File\UploadedFile $file */

                // Generate a unique name for the file before saving it
                $fileName = md5(uniqid()).'.'.$file->guessExtension();

                // Move the file to the directory where brochures are stored
                $brochuresDir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/brochures';
                $file->move($brochuresDir, $fileName);

                // Update the 'brochure' property to store the PDF file name
                // instead of its contents
                $product->setBrochure($fileName);
               // ... persist the $product variable or any other work
                $em = $this->getDoctrine()->getManager();
                $em->persist($product);
                $em->flush();
                return $this->redirect($this->generateUrl('app_product_list'));
            }
        }



        return $this->render('UploadFileBundle:Product:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }

}

我还建议使用VichUploaderBundle https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/index.md