Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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();

???


7.8k

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



12.9k
edited 9d ago

@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.



12.9k

@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.6k
Accepted
answer

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



7.8k

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 7d ago

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



12.9k

$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.