/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = SongComment::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, 'song_id' => $this->song_id, 'comment_id' => $this->comment_id, 'user_id' => $this->user_id, 'comment_type' => $this->comment_type, 'status' => $this->status, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'comment_content', $this->comment_content]); return $dataProvider; }
/** * Finds the SongComment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return SongComment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = SongComment::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionAddComment() { try { $post = Yii::$app->getRequest()->post(); if (empty($post['id'])) { throw new \Exception('获取页面数据丢失'); } if (!$this->findModel($post['id'])) { throw new \Exception('日记信息异常'); } $DiaryComment = new SongComment(); $DiaryComment->song_id = $post['id']; $DiaryComment->comment_id = isset($post['comment_id']) ? $post['comment_id'] : 0; $DiaryComment->comment_type = isset($post['comment_type']) ? $post['comment_type'] : 0; $DiaryComment->user_id = Yii::$app->getUser()->getId(); $DiaryComment->comment_content = $post['comment_content']; $DiaryComment->status = 1; $DiaryComment->created_at = date('Y-m-d H:i:s'); $DiaryComment->save(); $error = $DiaryComment->getErrors(); } catch (\Exception $exp) { Yii::$app->getSession()->setFlash('error', $exp->getMessage()); } return $this->redirect(['view', 'id' => $post['id']]); }