/** * Saves a comment into the database. * * @param \DeadPoolCave\Domain\Comment $comment The comment to save */ public function save(Comment $comment) { $commentData = array('art_id' => $comment->getArticle()->getId(), 'usr_id' => $comment->getAuthor()->getId(), 'com_content' => $comment->getContent()); if ($comment->getId()) { // The comment has already been saved : update it $this->getDb()->update('t_comment', $commentData, array('com_id' => $comment->getId())); } else { // The comment has never been saved : insert it $this->getDb()->insert('t_comment', $commentData); // Get the id of the newly created comment and set it on the entity. $id = $this->getDb()->lastInsertId(); $comment->setId($id); } }
/** * Article details controller. * * @param integer $id Article id * @param Request $request Incoming request * @param Application $app Silex application */ public function articleAction($id, Request $request, Application $app) { $article = $app['dao.article']->find($id); $genres = $app['dao.genre']->findAll(); $authors = $app['dao.author']->findAll(); $editors = $app['dao.editor']->findAll(); $authorsArticle = $app['dao.author']->findByArticle($id); $commentFormView = null; if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) { // A user is fully authenticated : he can add comments $comment = new Comment(); $comment->setArticle($article); $user = $app['user']; $comment->setAuthor($user); $commentForm = $app['form.factory']->create(new CommentType(), $comment); $commentForm->handleRequest($request); if ($commentForm->isSubmitted() && $commentForm->isValid()) { $app['dao.comment']->save($comment); $app['session']->getFlashBag()->add('success', 'Votre commentaire à été ajouté.'); } $commentFormView = $commentForm->createView(); } $comments = $app['dao.comment']->findAllByArticle($id); return $app['twig']->render('article.html.twig', array('article' => $article, 'authorsArticle' => $authorsArticle, 'genres' => $genres, 'authors' => $authors, 'editors' => $editors, 'comments' => $comments, 'commentForm' => $commentFormView)); }