/** * Displays a single Blog model. * @param integer $id * @return mixed */ public function actionView($id) { $comment = new Comment(); if ($comment->load(\Yii::$app->request->post())) { $comment->blog_id = $id; $comment->status = Comment::STATUS_PENDING; if ($comment->save()) { Yii::$app->session->setFlash('commentAdded'); return $this->redirect(''); } else { throw new \yii\base\UserException('Не удалось добавить комментарий, сообщите об этом администратору сайта.'); } } return $this->render('viewBlog', ['model' => $this->findModel($id), 'comment' => $comment]); }
public function search($params) { $query = Comment::find()->with('user')->with('blog'); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $this->addCondition($query, 'id'); $this->addCondition($query, 'title', true); $this->addCondition($query, 'body', true); $this->addCondition($query, 'created_at'); $this->addCondition($query, 'created_by'); $this->addCondition($query, 'updated_at'); $this->addCondition($query, 'updated_by'); $this->addCondition($query, 'blog_id'); return $dataProvider; }
echo $form->field($model, 'title', ['options' => ['maxlength' => 255], 'addon' => ['prepend' => ['content' => '<i class="glyphicon glyphicon-star"></i>']]])->label('Заголовок или Ваше имя'); ?> <?php if (!isset($blog_id)) { ?> <?php echo $form->field($model, 'blog_id')->widget(Select2::className(), ['data' => ["" => "--- Select ---"] + Blog::options(['status' => Blog::STATUS_ACTIVE])]); ?> <?php } ?> <?php if (!isset($blog_id)) { ?> <?php echo $form->field($model, 'status')->dropDownList(Comment::statusOptions()); ?> <?php } ?> <?php echo $form->field($model, 'body')->textArea(['rows' => 10, 'cols' => 30]); ?> <div class="clearfix"></div> <?php if (\yii::$app->user->isGuest) { ?> <div style="margin-top: 15px; "> <?php
/** * @return \yii\db\ActiveQuery */ public function getComments() { return $this->hasMany(Comment::className(), ['blog_id' => 'id']); }
/** * 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 ($id !== null && ($model = Comment::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }