/** * Creates an Comment object based on a DB row. * @param array $row The DB row containing Comment data. * @return \MicroCMS\Domain\Comment */ protected function buildDomainObject($row) { $comment = new Comment(); $comment->setId($row['com_id']); $comment->setContent($row['com_content']); if (array_key_exists('art_id', $row)) { // Find and set the associated article $articleId = $row['art_id']; $article = $this->articleDAO->find($articleId); $comment->setArticle($article); } if (array_key_exists('usr_id', $row)) { // Find and set the associated author $userId = $row['usr_id']; $user = $this->userDAO->find($userId); $comment->setAuthor($user); } return $comment; }
/** * 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); $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', 'Your comment was succesfully added.'); } $commentFormView = $commentForm->createView(); } $comments = $app['dao.comment']->findAllByArticle($id); return $app['twig']->render('article.html.twig', array('article' => $article, 'comments' => $comments, 'commentForm' => $commentFormView)); }
/** * Creates an Comment object based on a DB row. * * @param array $row The DB row containing Comment data. * @return \MicroCMS\Domain\Comment */ protected function buildDomainObject($row) { // Find the associated article $reservationId = $row['NUM_RESA']; $reservation = $this->reservationDAO->find($reservationId); $comment = new Comment(); $comment->setId($row['COM_ID']); $comment->setUserId($row['USER_ID']); $comment->setContent($row['COM_CONTENT']); $comment->setReservation($row['NUM_RESA']); return $comment; }
/** * Saves a comment into the database. * * @param \MicroCMS\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); } }
use MicroCMS\Form\Type\CommentType; use MicroCMS\Form\Type\ArticleType; use MicroCMS\Form\Type\UserType; use MicroCMS\Form\Type\GroupType; // Home page $app->get('/', function () use($app) { $articles = $app['dao.article']->findAll(); return $app['twig']->render('index.html.twig', array('articles' => $articles)); })->bind('home'); // Article details with comments $app->match('/article/{id}', function ($id, Request $request) use($app) { $article = $app['dao.article']->find($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', 'Your comment was succesfully added.'); } $commentFormView = $commentForm->createView(); } $comments = $app['dao.comment']->findAllByArticle($id); return $app['twig']->render('article.html.twig', array('article' => $article, 'comments' => $comments, 'commentForm' => $commentFormView)); })->bind('article'); // Login form