public function create() { $comment = new Comment(); $count = $comment->where('article_id=' . $_POST['article_id'])->count(); $data = $_POST; $data['created_at'] = date("Y-m-d H:i:s", time()); $data['updated_at'] = date("Y-m-d H:i:s", time()); if ($comment->add($data)) { // $mod = new Article; // $article = $mod->where('article_id='.$_POST['article_id'])->find(); // $msg = '文章:['.$article['title'].'] 下有新的评论:'.$_POST['content']; //评论成功后给管理员发邮件 // sendMail('*****@*****.**','有人发表评论',$msg); // session('home.success_msg','评论成功'); // //$this->success('评论成功'); // $this->redirect('Article/show',array('id'=>$_POST['article_id']), 0, ''); $item = array('ret' => 0, 'data' => $data, 'key' => $count + 1); $this->ajaxReturn($item); } else { $this->ajaxReturn(array('ret' => 1)); } }
/** * 同时删除相关表里面的数据 * $noNeedTrans表示不需要事务控制 * 返回值:true/false */ public function deleteById($id, $noNeedTrans = false) { $map['id'] = $id; $pariseList = $this->getPraiseList($id); $commentList = $this->getCommentList($id); $pariseTable = new PraiseMoodModel(); $commentTable = new CommentModel(); if (!$noNeedTrans) { $this->startTrans(); } while (list($key, $parise) = each($pariseList)) { if ($pariseTable->deleteByMoodId($id, true) === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } } while (list($key, $comment) = each($commentList)) { if ($commentTable->deleteById($comment['id'], true) === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } } if ($this->where($map)->delete() === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } if (!$noNeedTrans) { return $this->commit(); } else { return true; } }
/** *动作: 删除文章 **/ public function delete() { $article = new Article(); $comments = new Comment(); $id = $_GET['id']; $comments->where('article_id=' . $id)->delete(); $article->where('article_id=' . $id)->delete(); $data = array('ret' => 0, 'msg' => '删除成功'); $this->ajaxReturn($data); }
/** *动作: 删除评论 **/ public function delete() { $comment = new Comment(); $id = $_GET['id']; if ($comment->where('id=' . $id)->delete()) { $data = array('ret' => 0, 'msg' => '删除成功'); $this->ajaxReturn($data); } }
/** * 同时删除相关表里面的数据 * $noNeedTrans表示不需要事务控制 * 返回值:返回false表示删除失败,返回true表示删除成功 */ public function deleteById($id, $noNeedTrans = false) { $map['id'] = $id; $moodList = $this->getRelatedMoodById($id); $moodTable = new MoodListModel(); if (!$noNeedTrans) { $this->startTrans(); } while (list($key, $mood) = each($moodList)) { if ($moodTable->deleteById($mood['id'], true) === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } } $praiseList = $this->getRelatedPraiseById($id); $praiseTable = new PraiseMoodModel(); while (list($key, $praise) = each($praiseList)) { if ($praiseTable->deleteByUserId($praise['user_id'], true) === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } } $commentList = $this->getRelatedCommentById($id); $commentTable = new CommentModel(); while (list($key, $comment) = each($commentList)) { if ($commentTable->deleteById($comment['id'], true) === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } } $letterList = $this->getRelatedLetterById($id); $letterTable = new LetterListModel(); while (list($key, $letter) = each($letterList)) { if ($letterTable->deleteById($letter['id'], true) === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } } if ($this->where($map)->delete() === false) { if (!$noNeedTrans) { $this->rollback(); } return false; } if (!$noNeedTrans) { return $this->commit(); } else { return true; } }