How to use paginater on array data

#1

Hi there,

I am using CakePHP paginator on some screens and it working perfectly. On those screen i get data from only one table as per their model.

$where = array('order' => 'id desc', 'conditions' => $conditions);
$this->paginate = $where;

$this->set('my_events', $this->Paginator->paginate('MyEvents')); // Set in Model

No on one screen I get data from three tables separately and merge there data in on array. Now i can not figure it out that how can i use it in paginator. I am trying this

$result = array_merge($transaction_arr, $tickets_arr, $event_trans);
$event_arr[] = $result;

$this->set('event_buy_details', $event_arr);

I also try this
$this->set('event_buy_details', $this->Paginator->paginate($event_arr));

But nothing happened.

So how can I achieve this for paginator.
Hope you understand my question.

#2

What Cake version are you using?

Also,

"No on one screen I get data from three tables separately and merge there data in on array. "

Why cant you use objects? They are much faster. Paginator requires a collection object afaik

#3

The paginate-function requires the first parameter to to be of type \Cake\Datasource\RepositoryInterface (basically aka Table-classes) or \Cake\Datasource\QueryInterface (basically the object that represents your query against your database).
This makes sense to me as I see pagination mostly as a tool to improve load-times of the site and spare database-resources.

That means you cannot simply use the pagination for arrays - for “workaround” of your problem, you might make use of (nested) associations to get your 3 models into one Query-Object that you can use for paginate-function.

Could look like this:

$myEvents = $this->Events->find()->where(['location' => 'SomeCity'])->contain(['EventDetails', 'EventDetails.TicketShops']);
$myEvents = $this->Paginator->paginate();
$this->set(compact('myEvents'));

In this example you have the following associations defined:
Events -> EventDetails
EventDetails -> TicketShops

#4

The build in paginator is build for repository objects/data. You can use the collection class’ chunk method to emulate pagination with a array.

However, looking at what you are doing, you would probably be better using a union query, or 3 separate paginators on the same page.