/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tblikes::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'userid' => $this->userid, 'tbmessageid' => $this->tbmessageid, 'created_at' => $this->created_at]); return $dataProvider; }
/** * 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.'); } }
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!'); } }