/** * Creates an Comment object based on a DB row. * * @param array $row The DB row containing Comment data. * @return \NanarStore\Domain\Comment */ protected function buildDomainObject($row) { $comment = new Comment(); $comment->setId($row['com_id']); $comment->setContent($row['com_content']); $comment->setGrade($row['com_grade']); 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; }
// Home page $app->get('/', function () use($app) { $articles = $app['dao.article']->findAll(); $categories = $app['dao.category']->findAll(); return $app['twig']->render('index.html.twig', array('articles' => $articles, 'categories' => $categories)); }); // Article details with comments $app->match('/article/{id}', function ($id, Request $request) use($app) { $article = $app['dao.article']->find($id); $categories = $app['dao.category']->findAll(); $user = $app['security']->getToken()->getUser(); $commentFormView = null; $connected = null; if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) { // A user is fully authenticated : he can add comments $comment = new Comment(); $comment->setArticle($article); $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 a bien été ajouté.'); } $commentFormView = $commentForm->createView(); $connected = true; } $comments = $app['dao.comment']->findAllByArticle($id); return $app['twig']->render('article.html.twig', array('article' => $article, 'categories' => $categories, 'comments' => $comments, 'commentForm' => $commentFormView, 'connected' => $connected)); }); // Login form