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;
I also try this
But nothing happened.
So how can I achieve this for paginator.
Hope you understand my question.
What Cake version are you using?
"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
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();
In this example you have the following associations defined:
Events -> EventDetails
EventDetails -> TicketShops
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.