= Comment::find() - ->where([ - 'model' => $model->className(), - 'model_id' => $model->getPrimaryKey(), - ]) - ->count() ?>
+= count($model->comments) ?>
diff --git a/common/models/Blog.php b/common/models/Blog.php index aa79dae..6baf9b4 100644 --- a/common/models/Blog.php +++ b/common/models/Blog.php @@ -2,6 +2,7 @@ namespace common\models; + use common\modules\comment\models\Comment; use Yii; use yii\behaviors\BlameableBehavior; use yii\behaviors\TimestampBehavior; @@ -30,7 +31,6 @@ return 'blog'; } - /** * @inheritdoc */ @@ -49,11 +49,11 @@ 'value' => new Expression('NOW()'), ], 'slug' => [ - 'class' => 'common\behaviors\Slug', - 'in_attribute' => 'name', + 'class' => 'common\behaviors\Slug', + 'in_attribute' => 'name', 'out_attribute' => 'link', - 'translit' => true - ] + 'translit' => true, + ], ]; } @@ -64,7 +64,10 @@ { return [ [ - [ 'name', 'description' ], + [ + 'name', + 'description', + ], 'required', ], [ @@ -83,8 +86,9 @@ ]; } - public function getDateCreate(){ - return date('Y-m-d',strtotime($this->date_add)); + public function getDateCreate() + { + return date('Y-m-d', strtotime($this->date_add)); } /** @@ -104,4 +108,10 @@ 'cover' => Yii::t('app', 'Фото главное'), ]; } + + public function getComments() + { + return $this->hasMany(Comment::className(), [ 'model_id' => 'blog_id' ]) + ->where([ 'model' => $this->className() ]); + } } diff --git a/common/modules/comment/widgets/CommentWidget.php b/common/modules/comment/widgets/CommentWidget.php index 52c316f..cf8b2a3 100644 --- a/common/modules/comment/widgets/CommentWidget.php +++ b/common/modules/comment/widgets/CommentWidget.php @@ -197,12 +197,16 @@ if(empty( $view )) { throw new \yii\base\InvalidConfigException("form_options[view] must be set"); } - return $this->render($view, [ - 'model' => $this->comment_class, - 'rating' => $this->rating_class, - 'user' => \Yii::$app->user->identity, - 'dataProvider' => $this->dataProvider, - ]); + if($this->comment_class->guestComment || !empty(\Yii::$app->user->identity)) { + return $this->render($view, [ + 'model' => $this->comment_class, + 'rating' => $this->rating_class, + 'user' => \Yii::$app->user->identity, + 'dataProvider' => $this->dataProvider, + ]); + } else { + return ''; + } } public function renderWidget() diff --git a/common/modules/comment/widgets/views/form-project-comment.php b/common/modules/comment/widgets/views/form-project-comment.php index 240d241..5a76271 100644 --- a/common/modules/comment/widgets/views/form-project-comment.php +++ b/common/modules/comment/widgets/views/form-project-comment.php @@ -22,7 +22,7 @@
= Comment::find() - ->where([ - 'model' => $model->className(), - 'model_id' => $model->getPrimaryKey(), - ]) - ->count() ?>
+= count($model->comments) ?>
= Comment::find() - ->where([ - 'model' => $model->className(), - 'model_id' => $model->getPrimaryKey(), - ]) - ->count() ?>
+= count($model->comments) ?>