Is it possible to place and sort two different models in the same view in CakePHP?

I want to do something very direct and simple. I want two different sets of paginated data on one page. Two different sets depend on different models. For discussion, we will say that they are Image and Item .

I can configure two pagers for two models and get the correct set of objects. I can get the correct pager links. But when it comes to parameter references, both pagers read the parameters and assume that they apply to them.

It looks like this:

  $this->paginate = array ( 'Item'=>array( 'conditions'=>array('user_id'=>$id), 'limit' => 6, 'order' => array( 'Item.votes'=>'desc', 'Item.created'=>'desc' ), 'contain'=>array( 'User', 'ItemImage' => array ( 'order'=>'ItemImage__imageVotes desc' ) ) ), 'Image'=>array( 'limit'=>6, 'contain'=>array( 'User', 'ItemImage'=>array('Item'), ), 'order'=>array( 'Image.votes'=>'desc', 'Image.views'=>'desc' ), 'conditions'=>array( 'Image.isItemImage'=>1, 'Image.user_id'=>$id ) ) ); $this->set('items', $this->paginate('Item')); $this->set('images', $this->paginate('Image')); 

This is in the controller. In the view, I have a sort of links that look like this:

 <div class="control"><?php echo $this->Paginator->sort('Newest', 'Image.created', array('model'=>'Image')); ?></div> 

However, this gives a link that looks like this:

 http://localhost/profile/37/page:1/sort:Image.created/direction:asc 

There is nothing to talk about which model I intend to sort. So when I click on the link, it tries to sort both models using Image.created . The result is an error because Item cannot be sorted by Image.created . Is there something I'm doing wrong? Or is it something that CakePHP paginator is not supported?

+4
source share
2 answers

The simplest solution would be to implement both grids as elements that extract their own data, and use AJAX to load the elements into the page.

The only other option is to change the settings so that you pass the parameters for both grids to each grid when sorting or navigating through pages. The code posted by Leo above is a good start. You can add the model key from the paginate array to each named parameter and make sure that you pass all the url parameters to the paginate function, and you should move in the right direction.

+1
source

You need to override the paginate method for the controller model of this page.

I did something similar, maybe this snippet will help:

 function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { $pageParams = compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive', 'group'); $this->contain('ModuleType', 'NodeDescriptor'); $pageItems = $this->find('all',$pageParams); $pagesOut = array(); foreach($pageItems as $pageItem) { $status = $pageItem['SiteAdmin']['status_id']; $moduleInfo = null; $nodeTitle = $pageItem['NodeDescriptor']['title']; $published = $pageItem['NodeDescriptor']['published']; $pageitemID = $pageItem['SiteAdmin']['id']; $moduleId = $pageItem['SiteAdmin']['module_id']; $contName = $pageItem['ModuleType']['controller']; if($moduleId) { $thisModel = ClassRegistry::getObject($moduleType); $thisModel->contain(); $moduleInfo = $thisModel->read(null,$moduleId); $moduleInfo = $moduleInfo[$moduleType]; } $pagesOut[] = array( 'status'=>$status, 'node'=>$nodeTitle, 'published'=>$published, 'info'=>$moduleInfo, 'module_id'=>$moduleId, 'contName'=>$contName, 'pageitem_id'=>$pageitemID); } return $pagesOut; } 

Thus, you gain control over the parameters passed to paginate, so you can pass model data, control flags, etc.

+2
source

All Articles