-
-
Save Bakhshi-Faisal/ffa62cc6b059f662243cf7e2564a03ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use AppBundle\Entity\Files; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request; | |
/** | |
* File controller. | |
* | |
* @Route("files") | |
*/ | |
class FilesController extends Controller | |
{ | |
/** | |
* Lists all file entities. | |
* | |
* @Route("/", name="files_index") | |
* @Method("GET") | |
*/ | |
public function indexAction() | |
{ | |
$em = $this->getDoctrine()->getManager(); | |
$files = $em->getRepository('AppBundle:Files')->findAll(); | |
return $this->render('files/index.html.twig', array( | |
'files' => $files, | |
)); | |
} | |
/** | |
* Creates a new file entity. | |
* | |
* @Route("/new", name="files_new") | |
* @Method({"GET", "POST"}) | |
*/ | |
public function newAction(Request $request) | |
{ | |
$file = new File(); | |
$form = $this->createForm('AppBundle\Form\FilesType', $file); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($file); | |
$em->flush(); | |
return $this->redirectToRoute('files_show', array('id' => $file->getId())); | |
} | |
return $this->render('files/new.html.twig', array( | |
'file' => $file, | |
'form' => $form->createView(), | |
)); | |
} | |
/** | |
* Finds and displays a file entity. | |
* | |
* @Route("/{id}", name="files_show") | |
* @Method("GET") | |
*/ | |
public function showAction(Files $file) | |
{ | |
$deleteForm = $this->createDeleteForm($file); | |
return $this->render('files/show.html.twig', array( | |
'file' => $file, | |
'delete_form' => $deleteForm->createView(), | |
)); | |
} | |
/** | |
* Displays a form to edit an existing file entity. | |
* | |
* @Route("/{id}/edit", name="files_edit") | |
* @Method({"GET", "POST"}) | |
*/ | |
public function editAction(Request $request, Files $file) | |
{ | |
$deleteForm = $this->createDeleteForm($file); | |
$editForm = $this->createForm('AppBundle\Form\FilesType', $file); | |
$editForm->handleRequest($request); | |
if ($editForm->isSubmitted() && $editForm->isValid()) { | |
$this->getDoctrine()->getManager()->flush(); | |
return $this->redirectToRoute('files_edit', array('id' => $file->getId())); | |
} | |
return $this->render('files/edit.html.twig', array( | |
'file' => $file, | |
'edit_form' => $editForm->createView(), | |
'delete_form' => $deleteForm->createView(), | |
)); | |
} | |
/** | |
* Deletes a file entity. | |
* | |
* @Route("/{id}", name="files_delete") | |
* @Method("DELETE") | |
*/ | |
public function deleteAction(Request $request, Files $file) | |
{ | |
$form = $this->createDeleteForm($file); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->remove($file); | |
$em->flush(); | |
} | |
return $this->redirectToRoute('files_index'); | |
} | |
/** | |
* Creates a form to delete a file entity. | |
* | |
* @param Files $file The file entity | |
* | |
* @return \Symfony\Component\Form\Form The form | |
*/ | |
private function createDeleteForm(Files $file) | |
{ | |
return $this->createFormBuilder() | |
->setAction($this->generateUrl('files_delete', array('id' => $file->getId()))) | |
->setMethod('DELETE') | |
->getForm() | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* Files | |
* | |
* @ORM\Table(name="files") | |
* @ORM\Entity(repositoryClass="AppBundle\Repository\FilesRepository") | |
*/ | |
class Files | |
{ | |
/** | |
* @var int | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="file", type="string", length=255, unique=true) | |
* @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.") | |
* @Assert\File(mimeTypes={ "application/pdf" }) | |
*/ | |
private $file; | |
/** | |
* | |
* @return Files | |
*/ | |
function getUser() { | |
return $this->user; | |
} | |
/** | |
* Get id | |
* | |
* @return int | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set file | |
* | |
* @param string $file | |
* | |
* @return Files | |
*/ | |
public function setFile($file) | |
{ | |
$this->file = $file; | |
return $this; | |
} | |
/** | |
* Get file | |
* | |
* @return string | |
*/ | |
public function getFile() | |
{ | |
return $this->file; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class FilesType extends AbstractType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('file',CollectionType::class,array()); | |
}/** | |
* {@inheritdoc} | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'AppBundle\Entity\Files' | |
)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getBlockPrefix() | |
{ | |
return 'appbundle_files'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use AppBundle\Entity\User; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Component\HttpFoundation\Request; | |
/** | |
* User controller. | |
* | |
* @Route("user") | |
*/ | |
class UserController extends Controller | |
{ | |
/** | |
* Lists all user entities. | |
* | |
* @Route("/", name="user_index") | |
* @Method("GET") | |
*/ | |
public function indexAction() { | |
$em = $this->getDoctrine()->getManager(); | |
$users = $em->getRepository('AppBundle:User')->findAll(); | |
return $this->render('user/index.html.twig', array( | |
'users' => $users, | |
)); | |
} | |
/** | |
* Creates a new user entity. | |
* | |
* @Route("/new", name="user_new") | |
* @Method({"GET", "POST"}) | |
*/ | |
public function newAction(Request $request) { | |
$user = new User(); | |
$form = $this->createForm('AppBundle\Form\UserType', $user); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$attachments = $user->getFiles(); | |
if ($attachments) { | |
foreach($attachments as $attachment) | |
{ | |
$file = $attachment->getFile(); | |
var_dump($attachment); | |
$filename = md5(uniqid()) . '.' .$file->guessExtension(); | |
$file->move( | |
$this->getParameter('upload_path'), $filename | |
); | |
var_dump($filename); | |
$attachment->setFile($filename); | |
} | |
} | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($user); | |
$em->flush(); | |
return $this->redirectToRoute('user_show', array('id' => $user->getId())); | |
} | |
return $this->render('user/new.html.twig', array( | |
'user' => $user, | |
'form' => $form->createView(), | |
)); | |
} | |
/** | |
* Finds and displays a user entity. | |
* | |
* @Route("/{id}", name="user_show") | |
* @Method("GET") | |
*/ | |
public function showAction(User $user) { | |
$deleteForm = $this->createDeleteForm($user); | |
return $this->render('user/show.html.twig', array( | |
'user' => $user, | |
'delete_form' => $deleteForm->createView(), | |
)); | |
} | |
/** | |
* Displays a form to edit an existing user entity. | |
* | |
* @Route("/{id}/edit", name="user_edit") | |
* @Method({"GET", "POST"}) | |
*/ | |
public function editAction(Request $request, User $user) { | |
$deleteForm = $this->createDeleteForm($user); | |
$editForm = $this->createForm('AppBundle\Form\UserType', $user); | |
$editForm->handleRequest($request); | |
if ($editForm->isSubmitted() && $editForm->isValid()) { | |
$this->getDoctrine()->getManager()->flush(); | |
return $this->redirectToRoute('user_edit', array('id' => $user->getId())); | |
} | |
return $this->render('user/edit.html.twig', array( | |
'user' => $user, | |
'edit_form' => $editForm->createView(), | |
'delete_form' => $deleteForm->createView(), | |
)); | |
} | |
/** | |
* Deletes a user entity. | |
* | |
* @Route("/{id}", name="user_delete") | |
* @Method("DELETE") | |
*/ | |
public function deleteAction(Request $request, User $user) { | |
$form = $this->createDeleteForm($user); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->remove($user); | |
$em->flush(); | |
} | |
return $this->redirectToRoute('user_index'); | |
} | |
/** | |
* Creates a form to delete a user entity. | |
* | |
* @param User $user The user entity | |
* | |
* @return \Symfony\Component\Form\Form The form | |
*/ | |
private function createDeleteForm(User $user) { | |
return $this->createFormBuilder() | |
->setAction($this->generateUrl('user_delete', array('id' => $user->getId()))) | |
->setMethod('DELETE') | |
->getForm() | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Doctrine\Common\Collections\ArrayCollection; | |
/** | |
* User | |
* | |
* @ORM\Table(name="user") | |
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository") | |
*/ | |
class User | |
{ | |
/** | |
* @var int | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="name", type="string", length=255) | |
*/ | |
private $name; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="lastName", type="string", length=255) | |
*/ | |
private $lastName; | |
/** | |
* @ORM\ManyToMany(targetEntity="Files", cascade={"persist"}) | |
*/ | |
private $files; | |
function __construct() { | |
$this->files = new ArrayCollection(); | |
} | |
/** | |
* Get id | |
* | |
* @return int | |
*/ | |
public function getId() { | |
return $this->id; | |
} | |
/** | |
* Set name | |
* | |
* @param string $name | |
* | |
* @return User | |
*/ | |
public function setName($name) { | |
$this->name = $name; | |
return $this; | |
} | |
/** | |
* Get name | |
* | |
* @return string | |
*/ | |
public function getName() { | |
return $this->name; | |
} | |
/** | |
* Set lastName | |
* | |
* @param string $lastName | |
* | |
* @return User | |
*/ | |
public function setLastName($lastName) { | |
$this->lastName = $lastName; | |
return $this; | |
} | |
/** | |
* Get lastName | |
* | |
* @return string | |
*/ | |
public function getLastName() { | |
return $this->lastName; | |
} | |
/** | |
* Get files | |
* | |
* @return ArrayCollection | |
*/ | |
function getFiles() { | |
return $this->files; | |
} | |
/** | |
* Set files | |
* @param type $files | |
*/ | |
function setFiles($files) { | |
$this->files = $files; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\FileType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use AppBundle\Form\FilesType; | |
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | |
class UserType extends AbstractType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name') | |
->add('lastName') | |
->add('files',FileType::class, array('data_class' => null | |
)); | |
}/** | |
* {@inheritdoc} | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'AppBundle\Entity\User' | |
)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getBlockPrefix() | |
{ | |
return 'appbundle_user'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment