Exemplo n.º 1
0
 public function actionCheckAnswer()
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         $ac_models = new AnswerComments();
         $ac_models->message = $_POST['CommentForm']['message'];
         $ac_models->answer_id = $_POST['CommentForm']['comment_id'];
         $ac_models->uid = Yii::app()->user->id;
         $ac_models->time = time();
         if (!$ac_models->save()) {
             throw new Exception('评论失败');
         }
         //在question中的 comment_count字段+1
         if (!Answer::model()->updateByPk($ac_models->answer_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());
     }
 }
Exemplo n.º 2
0
 /**
  * 删除信息
  * 多表删除  事物处理
  * 删除 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());
     }
 }