yii2mod / yii2-comments

Comments module for Yii2
MIT License
159 stars 64 forks source link

How to show only the comments related to this activeRecord / url #55

Closed eduardo-g-silva closed 7 years ago

eduardo-g-silva commented 7 years ago

Hi Igor, Sorry to bored you with a silly question but I cant manage to filter the record comments all rest it is looking beautiful.

<?php
echo \yii2mod\comments\widgets\Comment::widget([
    'model' => $book,
    'commentView' => '@frontend/views/comments/index', // path to your template
    //'relatedTo' => 'User ' . \Yii::$app->user->identity->username . ' commented on the page ' . \yii\helpers\Url::current(),
    'relatedTo' => \yii\helpers\Url::current(),
    'maxLevel' => 2,
    // set `pageSize` with custom sorting
    'dataProviderConfig' => [
        'sort' => [
            'attributes' => ['id'],
            'defaultOrder' => ['id' => SORT_DESC],
        ],
        'pagination' => [
            'pageSize' => 10
        ],
    ],
    // your own config for comments ListView, for example:
    'listViewConfig' => [
        'emptyText' => Yii::t('app', 'No comments found.'),
    ]
]);
?>

I can see that I can change the relatedTo property with any value and this change the db field but I don't understand how the model in my case $book works with the widget

thanks

ihorchepurnyi commented 7 years ago

Hi, relatedTo column only used for filtering the comments in the admin panel.

eduardo-g-silva commented 7 years ago

Ok, so how do I show only the comments related to an specific activeRecord or url ?

Is it a way?

ihorchepurnyi commented 7 years ago

Show comments for specific model:

$model = PostModel::find()->where(['title' => 'some title'])->one();

Comments will be attached for this model.

ihorchepurnyi commented 7 years ago
<?php
$model = PostModel::find()->where(['title' => 'some title'])->one();
echo \yii2mod\comments\widgets\Comment::widget([
    'model' => $model,
]);
?>
ihorchepurnyi commented 7 years ago

Also, you can create you own dataProvider and pass it to CommentWidget.

ihorchepurnyi commented 7 years ago

I prefer the first example.

eduardo-g-silva commented 7 years ago

It is working fine, looks like is not my day for programming. If i provide the correct model it works as you said. It is a complex form and i was using another variable that i use for related books instead of the activeRecord.
Thanks again

ihorchepurnyi commented 7 years ago

You're welcome.