I'm building a dynamic view (Page) that consists of multiple elements (widgets) called via $this->element('messages_unread'). Some of these elements need data that is not related to the Page model.
In real life words: my users will be able to construct their own Page by choosing from a multitude of elements ("top 5 posts", "10 unread messages", etc...)

I get the data by calling $this->requestAction(array('controller'=>'events','action'=>'archive') from within the element, the url-variables differ per element .

I'm aware of the fact that requestAction() is expensive and I plan on limiting the costs by proper caching.

The actual question:
My problem is Pagination. When I'm in the Page view and call requestAction('/events/archive') the PaginatorHelper in the Page view will be unaware of the Event model and its paginator variables and $this->Paginator->next() etc... will not work.
How can I implement proper Pagination? I've tried to set the model by calling $this->Paginator->options(array('model'=>'Event')) but that doesn't work.
Do I maybe need to return custom defined Pagination variables in the requestAction and thus construct my own?

Or is there another approach that maybe even avoids requestAction()? And keep in mind here that the requested data is unrelated to the Page.

Kind regards, Bart

[Edit] My temporary solution but still open for comments/solutions:
In the requestedAction Event/archive, return paginator variables along with the data like this: return array('data'=>$this->paginate(), 'paging' => $this->params['paging']);

有帮助吗?

解决方案

I've tinkered a bit more and the following works for me, and the PaginationHelper works:

In the element:

// requestAction returns an array('data'=>... , 'paging'=>...)
$data = $this->requestAction(array('controller'=>'events','action'=>'archive'));  

// if the 'paging' variable is populated, merge it with the already present paging variable in $this->params. This will make sure the PaginatorHelper works
if(!isset($this->params['paging'])) $this->params['paging'] = array();
$this->params['paging'] = array_merge( $this->params['paging'] , $data['paging'] );

foreach($data['events'] as $event) {
    // loop through data...
}

In the Controller:

public function archive() {
    $this->paginate = array(
        'limit'     => 10
    );

    if ($this->params['requested'])
        return array('events'=>$this->paginate('Event'), 'paging' => $this->params['paging']);

    $this->set('events', $this->paginate('Event') );
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top