public function saveComment(Comment $comment) { $data = ['news_id' => $comment->getNewsId(), 'account_id' => $comment->getAccountId(), 'content' => $comment->getContent(), 'date_posted' => $comment->getDatePosted()]; $id = $comment->getid(); if ($id == 0) { $this->tableGateway->insert($data); } else { if ($this->getComment($id)) { $this->tableGateway->update($data, ['id' => $id]); } else { throw new \Exception('Comment id does not exist'); } } }
public function detailAction() { $name = ''; $account_id = 0; $session = $session = new \Zend\Session\Container('user'); if (isset($session) && !empty($session->name)) { $name = $session->name; $account_id = (int) $session->id; } $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('news'); } $comments = $this->getCommentTable()->getCommentsByNewsId($id); $form = new CommentForm(); $form->get('news_id')->setValue($id); $form->get('account_id')->setValue($account_id); $request = $this->getRequest(); if ($request->isPost()) { $comment = new Comment(); $form->setInputFilter($comment->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $comment->exchangeArray($form->getData()); $this->getCommentTable()->saveComment($comment); return $this->redirect()->toRoute('news', ['action' => 'detail', 'id' => $id]); } return ['errors' => $form->getMessages(), 'accountName' => $name, 'news' => $this->getNewsTable()->getNews($id), 'comments' => $comments, 'form' => $form]; } return ['accountName' => $name, 'news' => $this->getNewsTable()->getNews($id), 'comments' => $comments, 'form' => $form]; }