/** * Creates a new comment. * This method attempts to create a new comment based on the user input. * If the comment is successfully created, the browser will be redirected * to show the created comment. * @param News the news that the new comment belongs to * @return Comment the comment instance */ protected function newComment($model) { $comment = new Comment(); if ($comment->load(Yii::$app->request->post())) { /**验证通过***/ $comment->attributes = $_POST['Comment']; if (Yii::$app->user->isGuest) { return $comment; } else { $comment->author_id = Yii::$app->user->id; //评论者的id $comment->author_name = Yii::$app->user->getIdentity()->username; //评论者的名称 $comment->create_time = date("Y-m-d H:i:s", time()); if (Yii::$app->user->getIdentity()->role == 1) { $comment->status = 1; } else { $comment->status = 2; } //待审核 //$comment->post_id = $model->id;//文章id $comment->user_id = $model->author_id; //文章作者id $comment->classify_type = Humour::getClassName(); //分类 $comment->comment_parent_id = '0'; } if ($comment->author_id === '') { Yii::$app->Session->setFlash('commentSubmitted', '请先登录'); $this->refresh(); exit; } $comment->content = $_POST['Comment']['content']; if ($model->addComment($comment)) { if ($comment->status == Comment::STATUS_PENDING) { Yii::$app->Session->setFlash('commentSubmitted', '评论会在审核通过后显示。'); } $this->refresh(); } } return $comment; }