Beispiel #1
0
 public function actionDoFocus()
 {
     if (!Yii::app()->request->isAjaxRequest) {
         $this->error('非法请求');
     }
     $id = $_POST['id'];
     //首先查找问题是否存在
     if (QuestionFocus::model()->exists('uid=:uid AND question_id=:question_id', array(':uid' => Yii::app()->user->id, ':question_id' => $id))) {
         $is_focus = true;
     } else {
         $is_focus = false;
     }
     $data = array();
     if (!$is_focus) {
         $transaction = Yii::app()->db->beginTransaction();
         try {
             //执行插入
             $qf_model = new QuestionFocus();
             $qf_model->question_id = $id;
             $qf_model->uid = Yii::app()->user->id;
             $qf_model->add_time = time();
             if (!$qf_model->save()) {
                 throw new ErrorException('关注失败');
             }
             $this->incFocusCount($id);
             $transaction->commit();
             $data['mesg'] = '关注成功';
             $data['status'] = true;
         } catch (Exception $e) {
             $transaction->rollBack();
             $data['mesg'] = '关注失败';
             $data['status'] = false;
         }
         //取消关注
     } else {
         $transaction = Yii::app()->db->beginTransaction();
         try {
             //执行取消插入
             if (!QuestionFocus::model()->deleteAll('question_id=:question_id AND uid=:uid', array(':question_id' => $id, ':uid' => Yii::app()->user->id))) {
                 throw new Exception('取消关注失败');
             }
             $this->DecFocusCount($id);
             $transaction->commit();
             $data['mesg'] = '取消关注成功';
             $data['status'] = true;
         } catch (Exception $e) {
             $transaction->rollBack();
             $data['mesg'] = '取消关注失败';
             $data['status'] = false;
         }
     }
     echo json_encode($data);
 }