I
I
Igor Braduloff2018-08-29 10:31:30
Yii
Igor Braduloff, 2018-08-29 10:31:30

Why is pagination not working in Yii2?

Good afternoon! I use standard Yii2 pagination:
There is an array $records

Array
(
    [0] => Array
        (
            [RecipientLegalName] => AAA
            [Service Name] => BBB
            [Payment Identifier] => CCC

            [Payment Number] => DDD
            [Payment Account] => EEE
            [Recipient Sum, MDL] => FFF
            [Payment State] => Завершен
            [State Time] => 2018-08-01 00:30:05
        )

    [1] => Array
        (
            [RecipientLegalName] => QQQQ
            [Service Name] => WWWWW
            [Payment Identifier] => TTTT
            [Payment Account] => PPPP
            [Recipient Sum, MDL] => FFF
            [Payment State] => Завершен
            [State Time] => 2018-08-01 07:32:53
        )
)

How to use this array for pagination in the code below?
$pages = new Pagination(['totalCount' =>count($records)]);
$posts = $records->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('index',compact('records','pages','posts'));
Throws an error Call to a member function offset() on array

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mykola, 2018-08-29
@pandinus

Throws an error Call to a member function offset() on array

Because you "have an array of $records" and not a Query object.
For an array, you need to use something like array_slice() and pass an offset to it.
BUT, I've done something like this.
$records = [1, 2, 3, 4, 5];
$arrayDataProvider = new ArrayDataProvider();
$arrayDataProvider->setModels($records);
$posts = $arrayDataProvider->getModels(); // depends on items per page
$pages = $arrayDataProvider->getPagination();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question