/** * 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); } }
// Admin home page $app->get('/admin', function () use($app) { $articles = $app['dao.article']->findAll(); $comments = $app['dao.comment']->findAll(); $users = $app['dao.user']->findAll(); return $app['twig']->render('admin.html.twig', array('articles' => $articles, 'comments' => $comments, 'users' => $users)); })->bind('admin'); // Article details with comments and basket $app->match('/article/{id}', function ($id, Request $request) use($app) { $article = $app['dao.article']->find($id); $commentFormView = null; $basketFormView = null; if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) { // A user is fully authenticated : he can add comments and article $user = $app['user']; $comment = new Comment(); $comment->setArticle($article); $comment->setAuthor($user); $commentForm = $app['form.factory']->create(new CommentType(), $comment); $commentForm->handleRequest($request); $tmp = 2; $basket = $app['dao.basket']->findByUsrArt($user->getId(), $article->getId()); if ($basket == null) { $tmp = 1; $basket = new Basket(); $basket->setUsrid($user->getId()); $basket->setArtid($article->getId()); } $basketForm = $app['form.factory']->create(new BasketType(), $basket); $basketForm->handleRequest($request); if ($commentForm->isSubmitted() && $commentForm->isValid()) {