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