Weird result ActiveDataProvider

I’m using ActiveDataProvider tu return result of comments my function in widget is

public function run()
    {
        $pageSize = 3;

        $query = Comment::find()->andWhere(['file_id' => 47]);
        $comments = $query->orderBy(['id'=> SORT_ASC]);
        $count = $query->count();
        $dataProvider = new ActiveDataProvider([
            'query' => $comments,
            'pagination' => [
                'defaultPageSize' => $pageSize,
            ],
        ]);

        return $this->render('indexComment', [
            'dataProvider' => $dataProvider,
            'fileId' => $this->file,
            'objectId' => $this->object,
        ]);
    }

I’m getting only one result but I have 7 comments with same ‘file_id’ but if I set $pageSize = 5 I get 3 results if I set my $pageSize = 6; I get only one result if I set $pageSize = 10; I get all results if I set $pageSize = 7; I get all my results If I set $pageSize = 4 I get only 3 results

I tried :

'pagination' => [
          'pageSize' => $pageSize,
      ],

I tried :

$query = Comment::find();
$count = $query->count();
$pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => $pageSize]);
$comments = $query->offset($pagination->offset)
    ->limit($pagination->limit);
$dataProvider = new ActiveDataProvider([
    'query' => $comments,
]);

I tried :

$query = Comment::find();
$count = $query->count();
$pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => $pageSize]);
$comments = $query->offset($pagination->offset)
    ->limit($pagination->limit);
$dataProvider = new ActiveDataProvider([
    'query' => $comments,
    'pagination' => [
        'pageSize' => 3,
    ],
]);

My indexComment :

<?php 
    echo ListView::widget([
        'dataProvider' => $dataProvider,
        'itemOptions' => ['class' => 'comment-album-list'],
        'itemView' => function ($model, $key, $index, $widget) 
                    {
                        return $this->render('listcomment',['model' => $model, 'index' => $index, 'widget' => $widget, 'key' => $key, 'pageIndex' => $pageIndex]);
                    },
        'id' => 'div-comment',
        'options' => [
            'tag' => 'div',
            'class' => 'comment-div text-center',
            'id' => 'comment-container',
        ],
        'layout' => "{items}\n{pager}",
        'pager' => [
            'class' => LoadMorePager::class,
            'label' => 'Load more',
            'id' => 'ultra-loadmore',
        ], 
    ]);
?>

My listComment :

<div>
    <?= $model->message ?>
</div>

all returning same thing
I don’t understand.

        'pagination' => [
            'pageSize' => $pageSize,
        ],

return same results

I don’t know what you are trying to accomplish
but isn’t something like this what you need (from memory, I didn’t try it)

    $pageSize = 3;
    $query = Comment::find()->where(['file_id' => 47])->orderBy(['id'=> SORT_ASC]);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => $pageSize,
        ],
    ]);
(you can check the db calls with Yii debugger )

There is no problem it’s just return the result as I write

@bynd Could you tell us what you are doing in the ‘indexComment’ view? Are you showing the comments using a GridView or ListView with the data provider?

If so, the simple code that @tri has suggested should work:

ActiveDataProvider will ignore the limit and offset in the query, because they will be dynamically added to the query using the pagination.

I updated my question to add indexComment I’m using Widgets not controller

What do you get when you comment out ‘LoadMorePager’ ?

I think the problem lies in the handling of LoadMorePager, not in the data provider.

is returning the same think but on controller it’s working without any error but on widget it’s giving the results as in my question so I’m now working by controller instead of using widgets