/** * @Route("/gallery/photoAlbum/{id}") */ public function photoAlbumction(Request $request, $id) { $album = $this->getDoctrine()->getRepository('AppBundle:Album')->find($id); $userSession = $this->get('security.context')->getToken()->getUser()->getId(); if ($album->getUser() != $userSession) { return new Response("<h2>No tienes acceso a este album<h2>"); } // 1) build the form $photo = new Photo(); $photo->setAlbum($id); $photo->setUser($userSession); $photo->setTags(""); $form = $this->createForm(new PhotoType(), $photo); // 2) handle the submit (will only happen on POST) $form->handleRequest($request); if (!$form->isValid()) { echo $form->getErrorsAsString(); } if ($form->isValid() && $form->isSubmitted()) { //$file = $request->files->get('image'); $strm = fopen($photo->getFile()->getRealPath(), 'rb'); $var = file_get_contents($photo->getFile()->getPathname()); $photo->setImage($var); $em = $this->getDoctrine()->getManager(); $em->persist($photo); $em->flush(); return new RedirectResponse($this->generateUrl('app_photo_photoalbumction', array('id' => $id))); } $em = $this->getDoctrine()->getManager(); $query = $em->createQuery('SELECT p FROM AppBundle:Photo p, AppBundle:Album a WHERE p.album = :album AND a.user = :user')->setParameters(array('album' => $id, 'user' => $userSession)); $photos = $query->getResult(); return $this->render('default/addPhoto.html.twig', array('form' => $form->createView(), 'datos' => $photos)); }