使产品形式增加变量"形式"不存在错误

时间:2017-09-05 11:37:35

标签: php forms symfony doctrine-orm

enter image description here DefaultController.php 我收到错误'变量"形式"不存在错误'。这是我的 DefaultController.php ,我希望在数据库中添加产品名称,产品价格和产品图片。我无法在数据库中添加这些项目。

<?php

namespace AppBundle\Controller;
use AppBundle\Entity\users;
use AppBundle\Entity\Login;
use AppBundle\Entity\Product;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\Extension\Core\Type\FileType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 




class DefaultController extends Controller
{
/**
     * @Route("/forms", name="forms")
     */
    public function formsAction(Request $request)
    { 
      $student = new Product(); 
      $form = $this->createFormBuilder($student) 
         ->add('productname', TextType::class) 
         ->add('productprice', TextType::class) 
         ->add('fileinput', FileType::class, array('label' => 'Photo (png, jpeg)')) 
         ->add('save', SubmitType::class, array('label' => 'Submit')) 
         ->getForm(); 

      $form->handleRequest($request); 
      if ($form->isSubmitted() && $form->isValid()) { 
         $file = $student->getFileinput(); 
         $fileName = md5(uniqid()).'.'.$file->guessExtension(); 
         $file->move($this->getParameter('photos_directory'), $fileName); 
         $student->setFileinput($fileName); 
         return new Response("User photo is successfully uploaded."); 
      } else { 
         return $this->render('default/forms.html.twig', array( 
            'form' => $form->createView(), 
         )); 
      } 
   }   
}


**forms.html.twig**
This is my **forms.html.twig** here i am using form_widget.

这是我的 forms.html.twig ,我在这里使用form_widget。 这是我的 forms.html.twig ,我在这里使用form_widget。

{% block body %} 
   <h3>Student form</h3> 
   <div id="simpleform"> 
      {{ form_start(form) }} 
      {{ form_widget(form) }} 
      {{ form_end(form) }} 
   </div>   
{% endblock %}  

**Product.php**

This is my **Product.php** database entity.

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
 */
class Product
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="productname", type="string", length=255)
     */
    private $productname;

    /**
     * @var int
     *
     * @ORM\Column(name="productprice", type="integer")
     */
    private $productprice;

    /**
     * @var string
     *
     * @ORM\Column(name="fileinput", type="string", length=255)
     */
    private $fileinput;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set productname
     *
     * @param string $productname
     *
     * @return Product
     */
    public function setProductname($productname)
    {
        $this->productname = $productname;

        return $this;
    }

    /**
     * Get productname
     *
     * @return string
     */
    public function getProductname()
    {
        return $this->productname;
    }

    /**
     * Set productprice
     *
     * @param integer $productprice
     *
     * @return Product
     */
    public function setProductprice($productprice)
    {
        $this->productprice = $productprice;

        return $this;
    }

    /**
     * Get productprice
     *
     * @return int
     */
    public function getProductprice()
    {
        return $this->productprice;
    }

    /**
     * Set fileinput
     *
     * @param string $fileinput
     *
     * @return Product
     */
    public function setFileinput($fileinput)
    {
        $this->fileinput = $fileinput;

        return $this;
    }

    /**
     * Get fileinput
     *
     * @return string
     */
    public function getFileinput()
    {
        return $this->fileinput;
    }
}

0 个答案:

没有答案