naval naval - 26 days ago 8
Twig Question

Unable to Update record in Symfony 3 getting error

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 %}

Answer Source

add data_class attribute at null in your formtype