We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon4 How to do Model Pagination ?

I used to have below code for my phalcon3.4 now i strugling to understand how to do the same in the phalcon4 ?

use Phalcon\Paginator\Adapter\Model;

$paginator = new Model(
     [
      "data"  => Robots::find(),
      "limit" => 25,
      "page"  => $currentPage,
    ]
 );
$paginate = $paginator->getPaginate();

???


8.4k

open the terminal and use phalcon-devtools and run the command phalcon scaffold it will create alot of useful things based on your db tables and it includes pagination

if that didn't clear things up you could point where your issue exactly



13.4k
edited Nov '20

@talal424 My Issue is in the older version code as above is not working anymore in the phalcon4, I understand it now different, So i need to know instead of the "data" parameter, what should i use, i dont see any way of doing this,

$resultset = User::query()
        ->where(" status=:mode: ")
        ->bind(array('status'=>'active'))
        ->limit($limit)
        ->execute();

         $paginator = new \Phalcon\Paginator\Adapter\Model(array(
                "data" => $resultset,
                "model" => \App\Models\Users::class,
                "limit" => $limit,
                "page" => $numberPage
            ));

above dont work anymore in the phalcon4, please let me know how to do it in phalcon4 way?

If you want to use data parameter, you should use Native Array
https://docs.phalcon.io/4.0/fr-fr/pagination#array
You can use model if you want to make the find in your paginator.



13.4k

@corentin-begne I can't use find() in the paginator, because my pagination function is a global common function which i select as data as models. which worked perfectly in the phalcon3.4 now due to this new phalcon4 way, i have to re-write many of the functions which i think will give up the upgrade to 4 for now.



12.8k
Accepted
answer

So just do a toArray() on your resultset and use NativeArray



8.4k

unfortunately it has been changed to this way you have two options either rewrite your code to pass the find paramaters or as @corentin-begne suggested convert the results to an array and then pass it to the paginator

@corentin-begne I can't use find() in the paginator, because my pagination function is a global common function which i select as data as models. which worked perfectly in the phalcon3.4 now due to this new phalcon4 way, i have to re-write many of the functions which i think will give up the upgrade to 4 for now.

edited Nov '20

It's true the data parameter is only used for NativeArray. However, the documentation shows that if you're using Phalcon\Paginator\Adapter\Model, you would use the model parameter instead:

use Phalcon\Paginator\Adapter\Model;

$paginator = new Model(
     [
      "model"  => Robots::find(),
      "limit" => 25,
      "page"  => $currentPage,
    ]
 );
$paginate = $paginator->getPaginate();

https://docs.phalcon.io/4.0/en/pagination



13.4k

$resultset = User::query() is not same as User::find(), as i said i have to change all the functions if i have to convert it to use the new way because i had the pagination as a global function, not sure why they did change, anyhow thanks

It's true the data parameter is only used for NativeArray. However, the documentation shows that if you're using Phalcon\Paginator\Adapter\Model, you would use the model parameter instead:

use Phalcon\Paginator\Adapter\Model;

$paginator = new Model(
    [
     "model"  => Robots::find(),
     "limit" => 25,
     "page"  => $currentPage,
   ]
);
$paginate = $paginator->getPaginate();

https://docs.phalcon.io/4.0/en/pagination

If you're doing a QueryBuilder - there's a paginator for that too. Something is going to need to be updated.