public function voirAction(Photo $photo) { $em = $this->getDoctrine()->getManager(); //je crée mon évènement avec les bons paramètres /* $event = new AugmenteVuesEvent($photo, $this->getUser()); $this->get('event_dispatcher') ->dispatch(AugmenteVuesEvents::onViewPhoto, $event); $photo->setNbVues($event.getPhoto()); */ //on est limité à 1 vue par user et par photo if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) { if (!$photo->getUsersView()->contains($this->getUser())) { $photo->addUsersView($this->getUser()); $photo->setNbVues($photo->getNbVues() + 1); $em->persist($photo); $em->flush(); } } //on va récupérer tous les commentaires par ordre décroissant sur le champ createdAt $commentairesRecup = $em->getRepository('PersoGalerieBundle:Commentaire')->getCommentairesByPhotoDesc($photo); if (null != $this->getUser()) { $commentaire = new Commentaire(); $commentaire->setUser($this->getUser()); $form = $this->createForm(new CommentaireType(), $commentaire); $request = $this->get('request'); if ($request->getMethod() == 'POST') { $form->submit($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $photo->addCommentaire($commentaire); $em->persist($commentaire); $em->flush(); $flash = $this->get('translator')->trans('alert.info.commentOk'); $this->get('session')->getFlashBag()->add('success', $flash); return $this->redirect($this->generateUrl('perso_galerie_viewOne', array('slug' => $photo->getSlug()))); } } return $this->render('PersoGalerieBundle:Galerie:voir.html.twig', array('photo' => $photo, 'form' => $form->createView(), 'commentaires' => $commentairesRecup)); } return $this->render('PersoGalerieBundle:Galerie:voir.html.twig', array('photo' => $photo, 'commentaires' => $commentairesRecup)); }
/** * Add commentaire * * @param \Perso\GalerieBundle\Entity\Commentaire $commentaire * * @return Photo */ public function addCommentaire(\Perso\GalerieBundle\Entity\Commentaire $commentaire) { $this->commentaires[] = $commentaire; $commentaire->setPhoto($this); // On ajoute ceci return $this; }