ho il controller ProductController:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Product;
use AppBundle\Entity\ProductPhoto;
use AppBundle\Form\ProductType;
/**
* Product controller.
*
*/
class ProductController extends Controller
{
private $ProductPhoto;
public function __construct()
{
$this->ProductPhoto = new \AppBundle\Entity\ProductPhoto();
$this->ProductPhoto->setNoFile(10);
}
/**
* Lists all Product entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$entities = $em->getRepository('AppBundle:Product')->findAll();
return $this->render('AppBundle:Product:index.html.twig', array(
'entities' => $entities
));
}
/**
* Finds and displays a Product entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('AppBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render('AppBundle:Product:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to create a new Product entity.
*
*/
public function newAction()
{
$entity = new Product();
$form = $this->createForm(new ProductType($this->ProductPhoto->getNoFile()), $entity);
return $this->render('AppBundle:Product:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView()
));
}
/**
* Creates a new Product entity.
*
*/
public function createAction()
{
$entity = new Product();
$request = $this->getRequest();
$form = $this->createForm(new ProductType($this->ProductPhoto->getNoFile()), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->flush();
$files =$request->files->get('appbundle_producttype');
foreach($files['ProductPhoto'] as $k => $file){
$someNewFilename = date('Y-m-d-H-i-s').$file->getClientOriginalName();
$dir = __DIR__ . '/../../../../web/';
$form['ProductPhoto'][$k]->getData()->move($dir.'/upload/', $someNewFilename);
$entityPhoto = new ProductPhoto();
$em = $this->getDoctrine()->getEntityManager();
$entityPhoto->setProduct($entity);
$entityPhoto->setPath($someNewFilename);
$em->persist($entityPhoto);
$em->flush();
}
return $this->redirect($this->generateUrl('product_show', array('id' => $entity->getId())));
}
return $this->render('AppBundle:Product:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView()
));
}
/**
* Displays a form to edit an existing Product entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('AppBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$editForm = $this->createForm(new ProductType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('AppBundle:Product:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing Product entity.
*
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('AppBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$editForm = $this->createForm(new ProductType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('product_edit', array('id' => $id)));
}
return $this->render('AppBundle:Product:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Product entity.
*
*/
public function deleteAction($id)
{
$form = $this->createDeleteForm($id);
$request = $this->getRequest();
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('AppBundle:Product')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Product entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('product'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}$this->render('Product/index.html.twig')