/** * Creates a new Tblikes model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Tblikes(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function actionLike() { $data = Yii::$app->request->post(); $user = new Users(); $phone = Users::find()->select('id')->where(['phone' => $data['phone']])->one(); //var_dump(Users::findOne(['phone'=>$data['phone']])['id']); $info = Tblikes::findOne(['userid' => $phone['id'], 'tbmessageid' => $data['tbmessageid']]); if ($info) { echo json_encode(array('flag' => 0, 'msg' => 'Already like!')); } else { $model = new Tblikes(); $model->userid = $phone['id']; $model->tbmessageid = $data['tbmessageid']; $model->created_at = time(); //$model->save (); $tbmessage = $this->findModel($data['tbmessageid']); if (!$tbmessage) { return array('flag' => 0, 'msg' => 'no tbmessage with this id!'); } $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { if (!$model->save()) { throw new Exception("dsfgsdfg"); } $tbmessage->likecount++; if (!$tbmessage->save()) { throw new Exception("asdfgsdfg"); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); //var_dump("133435465"); //Yii::$app->log->logger-> return array('flag' => 0, 'msg' => 'like fail!'); } // $to=Message::findOne(['id'=>$data['msgid']]); // $model2=new Notify(); // $model2->from=$phone['id']; // $model2->to=$to['userid']; // $model2->kind='点赞'; // $model2->created_at=time(); // $model2->msg_id=$data['msgid']; // $model2->save(); return array('flag' => 1, 'msg' => 'like success!'); } }