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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀