dongmangwei3822 2017-09-22 12:50
浏览 45
已采纳

无法更新Symfony 3中的记录获取错误

This is my DefaultController.php. I want to edit my form but am unable to do so. I am getting an error. I have attached error screenshot also. I have seen many examples but unable to do.

<?php


namespace AppBundle\Controller;
use AppBundle\Entity\users;
use AppBundle\Entity\Login;
use AppBundle\Entity\Product;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
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;


class DefaultController extends Controller
{

    /**
     * @Route("/edit/{id}", name="edit_user")
     */

    public function editAction($id, Request $request)
    {

            $user = $this->getDoctrine()
            ->getRepository('AppBundle:Product')
            ->find($id);

            $user->setProductname($user->getProductname());
            $user->setProductprice($user->getProductprice());
            $user->setFileinput($user->getFileinput());

            $user->setFileinput1($user->getFileinput1());
            $user->setFileinput2($user->getFileinput2());
            $user->setFileinput3($user->getFileinput3());


        //$user = new Product();
        /* $user->setProductname('pn');
        $user->setProductprice('pp');
        $user->setFileinput('fi');  */

        $form = $this->createFormBuilder($user)

            //->add('id', TextType::class)
            ->add('productname', TextType::class, array('label' => 'Product Name'))
            ->add('productprice', TextType::class, array('label' => 'Product Price'))
            ->add('fileinput', FileType::class, array('label' => 'Photo (png, jpeg)', 'attr' => array('class' => 'fieldClass')))

            ->add('fileinput1', FileType::class, array('label' => 'Photo (png, jpeg)', 'attr' => array('class' => 'fieldClass')))

            ->add('fileinput2', FileType::class, array('label' => 'Photo (png, jpeg)', 'attr' => array('class' => 'fieldClass')))

            ->add('fileinput3', FileType::class, array('label' => 'Photo (png, jpeg)', 'attr' => array('class' => 'fieldClass')))

            //->add('Register', SubmitType::class, array('label' => 'Create Task'))
            ->getForm();

            $form->handleRequest($request);

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

            //$id=$form['id']->getData();   
            $pn=$form['productname']->getData();
            $pp=$form['productprice']->getData();
            $fi=$form['fileinput']->getData();

            $fi1=$form['fileinput1']->getData();
            $fi2=$form['fileinput2']->getData();
            $fi3=$form['fileinput3']->getData();





         $fileName =$fi->getClientOriginalName(); 
         $fi->move($this->getParameter('photos_directory'), $fileName); 

         $fileName1 =$fi1->getClientOriginalName(); 
         $fi1->move($this->getParameter('photos_directory'), $fileName1); 

         $fileName2 =$fi2->getClientOriginalName(); 
         $fi2->move($this->getParameter('photos_directory'), $fileName2); 

         $fileName3 =$fi3->getClientOriginalName(); 
         $fi3->move($this->getParameter('photos_directory'), $fileName3); 

            $em = $this->getDoctrine()->getManager();
            $user = $em->getRepository('AppBundle:Product')->find($id);

            //$user->setFileinput($fi);
            $user->setProductname($pn);
            $user->setProductprice($pp);
            $user->setFileinput($fileName);

            $user->setFileinput1($fileName1);
            $user->setFileinput2($fileName2);
            $user->setFileinput3($fileName3);


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





        return $this->redirect($this->generateUrl('typography')); 

    }


        return $this->render('default/edit.html.twig', array(
        'user' => $user, 
        'form' => $form->createView()

        ));

    }
}

Product.php

This is my Product.php file I have mentioned my all entity in this file.

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


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


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


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



    /**
     * 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;
    }


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

        return $this;
    }

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


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

        return $this;
    }

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



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

        return $this;
    }

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


}

edit.html.twig This is my edit.html.twig

enter image description here

{% extends 'base.html.twig' %}

{% block body %}

<h1>EDIT PRODUCT</h1>

{{ form_start(form) }}

                {{ form_widget(form) }}
{{ form_end(form) }}

{% endblock %}
  • 写回答

2条回答 默认 最新

  • douxu3315 2017-09-22 13:48
    关注

    add data_class attribute at null in your formtype

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移