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

Pagination issue

Code:

$db = \Phalcon\DI::getDefault()->get('db');
$stmt = $db->prepare("SELECT id,first_name,last_name,contact_email from contacts where flag=1 UNION ALL SELECT id,first_name,last_name,bounce_email_address from mailchimp_import_contacts where flag=1");
$stmt->execute();
$contacts = $stmt->fetchAll(\PDO::FETCH_ASSOC);

When I excuted above i am getting issue like: Invalid data for paginator.

$paginator = new Phalcon\Paginator\Adapter\Model(array(
"data" => $contacts,    //Data to paginate
"limit" => 30,           //Rows per page
"page" => $numberPage   //Active page
));

PLs help to fix it...



98.9k
Accepted
answer

You have to pass a valid resultset to the paginator:

use Phalcon\Mvc\Model\Resultset\Simple as Resultset;

$db = \Phalcon\DI::getDefault()->get('db');
$result = $db->query("SELECT id,first_name,last_name,contact_email from contacts where flag=1 UNION ALL SELECT id,first_name,last_name,bounce_email_address from mailchimp_import_contacts where flag=1");

$contacts = new Resultset(null, new Contacts, $result);

$paginator = new Phalcon\Paginator\Adapter\Model(array(
"data" => $contacts,    //Data to paginate
"limit" => 30,           //Rows per page
"page" => $numberPage   //Active page
));