/** * Creates a new Tbreplys model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Tbreplys(); 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 actionReply() { $data = Yii::$app->request->post(); //$user=new Users(); $fromphone = Users::find()->select('id')->where(['phone' => $data['fphone']])->one(); //var_dump($fromphone); $model = new Tbreplys(); if ($data['tphone'] == '') { $model->toid = 0; } else { $tophone = Users::find()->select('id')->where(['phone' => $data['tphone']])->one(); //var_dump($tophone); $model->toid = $tophone['id']; } // $to=Messages::findOne(['id'=>$data['msgid']]); // if($fromphone['id']!=$to['id']){ // $model3=new Notify(); // $model3->from=$fromphone['id']; // $model3->to=$to['userid']; // //$model3->kind='评论'; // $model3->kind=$data['content']; // $model3->created_at=time(); // $model3->msg_id=$data['msgid']; // if(!$model3->save()){ // echo json_encode ( array ( // 'flag' => 0, // 'msg' => 'Reply failed!' // )); // return; // } // } $model->fromid = $fromphone['id']; $model->tbmessageid = $data['tbmessageid']; $model->content = $data['content']; $model->isread = 0; $model->created_at = time(); //var_dump($model); $tbmessage = $this->findModel($data['tbmessageid']); $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { if (!$model->save()) { throw new Exception("dsfgsdfg"); } $tbmessage->replycount++; 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' => 'Reply fail!'); } return array('flag' => 1, 'msg' => 'Reply success!'); }