예제 #1
0
 /**
  * Finds the Tblikes model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Tblikes the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Tblikes::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
예제 #2
0
 public function actionCancellike()
 {
     $data = Yii::$app->request->post();
     $user = new Users();
     $phone = $user->find()->select('id')->where(['phone' => $data['phone']])->one();
     $info = Tblikes::findOne(['userid' => $phone['id'], 'tbmessageid' => $data['tbmessageid']]);
     $tbmessage = $this->findModel($data['tbmessageid']);
     if (!$tbmessage) {
         return array('flag' => 0, 'msg' => 'no tbmessage with this id!');
     }
     if ($info) {
         //var_dump($tbmessage);
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try {
             if (!$info->delete()) {
                 throw new Exception("dsfgsdfg");
             }
             $tbmessage->likecount--;
             if (!$tbmessage->save()) {
                 throw new Exception("asdfgsdfg");
             }
             $transaction->commit();
         } catch (Exception $e) {
             $transaction->rollBack();
             return array('flag' => 0, 'msg' => 'Cancel fail!');
         }
         return array('flag' => 1, 'msg' => 'Cancel success');
     } else {
         return array('flag' => 0, 'msg' => 'Already Canceled!');
     }
 }