/** * @Route("/gallery/albums") */ public function albumsAction(Request $request) { $userSession = $this->get('security.context')->getToken()->getUser()->getId(); $album = new Album(); $album->setUser($userSession); $form = $this->createForm(new AlbumType(), $album); // 2) handle the submit (will only happen on POST) $form->handleRequest($request); if (!$form->isValid()) { echo $form->getErrorsAsString(); } if ($form->isValid() && $form->isSubmitted()) { $em = $this->getDoctrine()->getManager(); $em->persist($album); $em->flush(); return new RedirectResponse($this->generateUrl('app_album_albums', array('id' => $userSession))); //return new Response("<html>OK</html>"); } $em = $this->getDoctrine()->getManager(); $query = $em->createQuery('SELECT p FROM AppBundle:Album p WHERE p.user = :user')->setParameter('user', $userSession); $albums = $query->getResult(); return $this->render('default/ViewAlbums.html.twig', array('datos' => $albums, 'form' => $form->createView())); }
public function load(ObjectManager $manager) { for ($i = 0; $i < 5; $i++) { $album = new Album(); $album->setName('Album ' . $i); $manager->persist($album); $max = $i === 0 ? 5 : 21; for ($n = 0; $n < $max; $n++) { $image = new Image(); $image->setAlbum($album); $manager->persist($image); } } $manager->flush(); }
public function testConstruct() { $album = new Album(); $this->assertInstanceOf('\\DateTime', $album->getDate()); $this->assertInstanceOf('\\DateTime', $album->getCreationDate()); $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $album->getPhotos()); $this->assertInstanceOf('Doctrine\\Common\\Collections\\ArrayCollection', $album->getComments()); $this->assertTrue($album->isPublic()); }
private function canDelete(Album $album, User $user = null) { return $album->getAuthors()->contains($user); }
/** * Add album * * @param \AppBundle\Entity\Album $album * * @return Band */ public function addAlbum(\AppBundle\Entity\Album $album) { $this->albums[] = $album; $album->setBand($this); return $this; }
/** * @Route("/album") * @Method({"POST", "OPTIONS"}) */ public function postAlbumAction(Request $request) { $title = $request->request->get('title'); $description = $request->request->get('description'); $dateString = $request->request->get('date'); $authorsIds = explode(',', $request->request->get('authorsIds')); try { $date = new \DateTime($dateString); } catch (\Exception $e) { return new JsonResponse(array('message' => 'Invalid arguments.', 'list' => array('date: Unable to parse string.')), 422); } $em = $this->getDoctrine()->getManager(); $album = new Album(); $album->setTitle($title); $album->setDescription($description !== null ? $description : ''); $album->setDate($date); $album->setCreationDate(new \DateTime()); $album->addAuthor($this->getUser()); $authors = $em->getRepository('AppBundle:User')->findById($authorsIds); foreach ($authors as $author) { $album->addAuthor($author); } $validator = $this->get('validator'); $errors = $validator->validate($album); if (count($errors) > 0) { return new JsonResponse(Util::violationListToJson($errors), 422); } $em->persist($album); $em->flush(); return new JsonResponse($album->toJson()); }
/** * @param Album $album * @return Query */ public function getPaginationQuery(Album $album) { return $this->createQueryBuilder('Image')->select('Image')->where('Image.albumId = :albumId')->setParameter('albumId', $album->getId())->getQuery(); }