Под выбранной статьей блога по id ( таблица в бд blog)выводятся комменты(таблица в бд comment), таблица comment ссылается на blog по внешнему ключу blog_id. Комменты сразу должны появляться на этой же странице после отправления- и они появляются c отключенной верификацией. Как это исправить?
view
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\widgets\Pjax;
use yii\widgets\ActiveForm;
?>
<section>
<div class="container">
<div class="blog-post-area">
<h2 class="title text-center">Блог</h2>
<div class="single-blog-post">
<h3><?=$blog->name ?></h3>
<div class="post-meta">
<ul>
<li><i class="fa fa-calendar"></i> <?=$blog->date ?></li>
</ul>
</div>
<?php $mainImg = $blog->getImage();?>
<a>
<?= Html::img($mainImg->getUrl(), ['alt' => $blog->name])?>
</a>
<p><?=$blog->text?></p>
</div>
</div><!--/blog-post-area-->
<div class="response-area">
<h2>Комментарии</h2>
<?php Pjax::begin(['id' => 'notes']) ?>
<ul class="media-list">
<?php foreach($comments as $commen): ?>
<li class="media">
<div class="media-body">
<ul class="sinlge-post-meta">
<li><i class="fa fa-user"></i><?= $commen->name ?></li>
<li><i class="fa fa-calendar"></i><?= $commen->date ?></li>
</ul>
<p><?= $commen->text ?></p>
</div>
</li>
<?php endforeach;?>
</ul>
<?php Pjax::end() ?>
</div><!--/Response-area-->
<div class="replay-box">
<div class="row">
<?php Pjax::begin(['id' => 'new_note']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true, 'class' => 'replay-box row']]); ?>
<div class="col-sm-12">
<h2>Оставьте комментарий: </h2>
<div class="blank-arrow">
<label>Ваше имя</label>
</div>
<span>*</span>
<?= $form->field($comment, 'name')->textInput(['maxlength' => true])->label(false, ['style'=>'display:none']) ?>
<div class="blank-arrow">
<label>Ваша электронная почта</label>
</div>
<span>*</span>
<?= $form->field($comment, 'email')->textInput(['maxlength' => true])->label(false, ['style'=>'display:none']) ?>
<div class="text-area">
<div class="blank-arrow">
<label>Ваш комментарий</label>
</div>
<span>*</span>
<?= $form->field($comment, 'text')->textarea(['rows' => 11])->label(false, ['style'=>'display:none']) ?>
<a> <?= Html::submitButton('Отправить комментарий', ['class' => 'get_discount btn']) ?></a>
</div>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end() ?>
</div>
</div>
<?php
?>
</div>
</section>
<?php
?>
Код: Выделить всё
public function actionView($id)
{
$blog = Blog::findOne($id);
$comments= Comment::find()->where(['blog_id' => $id])->all();
$comment=new Comment();
$comment->blog_id = $id;
if( $comment->load(Yii::$app->request->post()) ) {
// $comment->blog_id = $id;
$comment->save(false);
}
// if(Yii::$app->request->isPjax){
// return $this->render('view', compact('comments'));
// }
return $this->render('view', compact('blog', 'comments', 'comment'));
}
Код: Выделить всё
$("document").ready(function(){
$("#new_note").on("pjax:end", function() {
$.pjax.reload({container:"#notes"}); //Reload GridView
});
});
Код: Выделить всё
<?php
namespace app\models;
use yii\db\ActiveRecord;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "comment".
*
* @property int $id
* @property int $blog_id
* @property string $name
* @property string $email
* @property string $text
* @property string $date
*
* @property Blog $blog
*/
class Comment extends \yii\db\ActiveRecord
{
public $verifyCode;
public function behaviors(){
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['date'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['date'],
],
// если вместо метки времени UNIX используется datetime:
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'comment';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['blog_id', 'name', 'email', 'text'], 'required'],
[['blog_id'], 'integer'],
[['text'], 'string'],
[['date'], 'safe'],
[['name', 'email'], 'string', 'max' => 100],
[['blog_id'], 'exist', 'skipOnError' => true, 'targetClass' => Blog::className(), 'targetAttribute' => ['blog_id' => 'id']],
['verifyCode', 'captcha'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'blog_id' => 'ID статьи',
'name' => 'Имя',
'email' => 'Электронная почта',
'text' => 'Текст',
'date' => 'Дата',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getBlog()
{
return $this->hasOne(Blog::className(), ['id' => 'blog_id']);
}
}
Код: Выделить всё
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "blog".
*
* @property int $id
* @property string $name
* @property string $description
* @property string $text
*
* @property Comment[] $comments
*/
class Blog extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
'image' => [
'class' => 'rico\yii2images\behaviors\ImageBehave',
]
];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'blog';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'description', 'text'], 'required'],
[['text'], 'string'],
[['name'], 'string', 'max' => 100],
[['description'], 'string', 'max' => 250],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Название',
'description' => 'Описание',
'text' => 'Текст',
'image' => 'Фото',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getComments()
{
return $this->hasMany(Comment::className(), ['blog_id' => 'id']);
}
}