/** * @Route("/auto/create", name="auto_create") */ public function createAutoAction(Request $request) { $auto = new Auto(); $form = $this->createForm(AddAutoType::class, $auto); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $auto = $form->getData(); $auto->setCreatedAt(new \DateTime(date('Y-m-d H:i:s'))); $auto->setUpdatedAt(new \DateTime(date('Y-m-d H:i:s'))); $file = $auto->getImage(); $fileName = md5(uniqid()) . '.' . $file->guessExtension(); $imageDir = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/auto/' . $auto->getBrand(); $file->move($imageDir, $fileName); $image = new Image(); $image->setTitle($auto->getBrand() . ' image'); $image->setImage($fileName); $image->setMain('1'); $auto->addImage($image); $em = $this->getDoctrine()->getManager(); $em->persist($image); $em->persist($auto); $em->flush(); return $this->redirectToRoute('auto_create_success', ['id' => $auto->getId()]); } return $this->render('default/auto_create_form.html.twig', array('form' => $form->createView())); }