public function run() { $commentForm = $this->createComment(); if (isset(Yii::$app->session['email']) && isset(Yii::$app->session['email'])) { $commentForm->email = Yii::$app->session['email']; $commentForm->username = Yii::$app->session['username']; } $comments = Comment::find()->where(['entity' => $this->entity, 'status' => 1])->tree(); echo $this->render('comments', compact('comments', 'commentForm')); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Comment::find()->orderBy('created_at DESC'); $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, 'parent_id' => $this->parent_id, 'level' => $this->level, 'created_by' => $this->created_by, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'ip', $this->ip])->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
public function create() { if (!Yii::$app->user->isGuest) { $this->created_by = Yii::$app->user->id; } if ($this->validate()) { return Yii::$app->db->transaction(function () { $comment = new Comment(); $comment->setAttributes($this->attributes, false); if (empty($this->parent_id)) { $comment->level = 0; } else { $comment->level = Comment::findOne(['id' => $this->parent_id])->level; $comment->level++; } $comment->status = 1; $comment->created_at = (new \DateTime())->format('Y-m-d H:i:s'); $comment->save(false); return true; }); } return false; }
public function run() { $comments = Comment::find()->where(['status' => 1])->orderBy('created_at DESC')->limit(10)->all(); echo $this->render('last-comments', compact('comments')); }
/** * Finds the Comment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Comment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Comment::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }