public function actionCheckQuestion() { $transaction = Yii::app()->db->beginTransaction(); try { $qc_models = new QuestionComments(); $qc_models->message = $_POST['CommentForm']['message']; $qc_models->question_id = $_POST['CommentForm']['comment_id']; //查看该问题是否已经被锁定 if ($this->is_lock($qc_models->question_id)) { throw new Exception("该问题已经被锁定!"); } $qc_models->uid = Yii::app()->user->id; $qc_models->time = time(); if (!$qc_models->save()) { throw new Exception('评论失败'); } //在question中的 comment_count字段+1 if (!Question::model()->updateByPk($qc_models->question_id, array('comment_count' => new CDbExpression('comment_count+1')))) { throw new ErrorException('评论失败'); } $transaction->commit(); $this->redirect(Yii::app()->request->urlReferrer); //$this->success('评论成功'); } catch (Exception $e) { $transaction->rollBack(); //exit($e->getMessage()); $this->error($e->getMessage()); } }
/** * 删除信息 * 多表删除 事物处理 * 删除 question 中信息 * 删除 answer 表中信息 * 删除 question * 更新topic中次数 */ public function actionDelete($id) { $transaction = Yii::app()->db->beginTransaction(); try { //删除question 表信息 if (!Question::model()->deleteByPk($id)) { throw new Exception("删除question表失败"); } //删除 topic_question 表 首先把 topic id 查找出来 //获取topic id $topic_ids = TopicQuestion::model()->findAll(array('select' => 'topic_id', 'condition' => 'question_id=:question_id', 'params' => array(':question_id' => $id))); //更新 topic次数 foreach ($topic_ids as $model) { //更新topic 次数 if (!Topic::model()->updateByPk($model->topic_id, array('discuss_count' => new CDbExpression('discuss_count-1')))) { throw new ErrorException('更新失败'); } } //删除topic_question 表中信息 if (false === TopicQuestion::model()->deleteAll('question_id=:question_id', array('question_id' => $id))) { throw new ErrorException('删除topic_question中信息失败'); } //删除question_comment 中信息 if (false === QuestionComments::model()->deleteAll('question_id=:question_id', array('question_id' => $id))) { throw new ErrorException('删除question_comment中信息失败'); } //获取answer_comment 中id $answer_comment_models = Answer::model()->findAll(array('select' => 'id', 'condition' => 'question_id=:question_id', 'params' => array(':question_id' => $id))); //删除answer_comment 中信息 foreach ($answer_comment_models as $answer_comment_model) { if (false === AnswerComments::model()->deleteAll('answer_id=:answer_id', array(':answer_id' => $answer_comment_model->id))) { throw new ErrorException('删除answer_comment中信息失败'); } } //删除answer 表中信息 if (false === Answer::model()->deleteAll('question_id=:question_id', array('question_id' => $id))) { throw new ErrorException('删除answer中信息失败'); } //删除question_focus 中信息 if (false === QuestionFocus::model()->deleteAll('question_id=:question_id', array('question_id' => $id))) { throw new ErrorException('删除question_focus 中信息失败'); } $transaction->commit(); $this->success('删除成功'); } catch (Exception $e) { $transaction->rollBack(); exit($e->getMessage()); $this->error($e->getMessage()); } }