Cakephp two separate pages of the same model

I have no idea how to handle these two separate pages in one view. Thanks for the help.

Controller Code:

[...]
$this->Post->bindModel(array(
'hasAndBelongsToMany'=>array('Tag'),
'belongsTo'=>array('User')),false);
if($this->Session->check('Auth.User.id'))
  $this->Post->bindModel(array(
  'hasMany'=>array(
    'UserVote'=>array('conditions'=>array('UserVote.user_id'=>$this->Session->read('Auth.User.id') )),
    'Favorite'=>array('conditions'=>array('Favorite.user_id'=>$this->Session->read('Auth.User.id') ))
  )), false);


$posts = $this->paginate($this->Post,array('Post.public'=>1,'Post.user_id'=>$uid));

$posts2 = $this->paginate($this->Post,array('Post.public'=>0,'Post.user_id'=>$uid));


$this->set('posts',$posts);
$this->set('posts2',$posts2);

[...]
+3
source share
4 answers

Although in most cases, the correct solution would be to rebuild your user interface to eliminate the need for double pagination, the following working solution:

First, in your controller, you override the cake paginate () function to find the delimiter key:

    /**
     * Handles automatic pagination of model records.
     *
     * @param mixed $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
     * @param mixed $scope Conditions to use while paginating
     * @param array $whitelist List of allowed options for paging
     * @return array Model query results
     * @access public
     * @link http://book.cakephp.org/view/165/Controller-Setup
     */
     function paginate($object = null, $scope = array(), $whitelist = array(), $key = null) {
      $results = parent::paginate($object, $scope, $whitelist);
      if ($key) {
       $this->params['paging'][$key] = $this->params['paging'][$object];
       unset($this->params['paging'][$object]);
      }

      return $results;
     }

Then

   /**
    * undocumented function
    *
    * @param string $key 
    * @return void
    * @access public
    */
     function _pageForPagination($by) {
       $page = 1;
       $samekey = isset($this->params['named']['by']) && $this->params['named']['by'] == $by;
       $pageInUrl = isset($this->params['named']['page']);
       if ($samekey && $pageInUrl) {
         $page = $this->params['named']['page'];
       }

       $this->passedArgs['page'] = $page;
       return $page;
     }

        /**
     * FIXME: Wrapper for Cake pagination
     * Change pagination criteria on the fly (conditions, grouping, order, limit)
     *
     * @param string $model 
     * @param string $criteria 
     * @return void
     * @author Andrew
     */
     function _paginateBy($key) {
      $this->User->unbindModel(array('hasMany' => array('UserImage')), false);
      $this->paginate['User'] = am($this->User->getCriteria($key), array('page' => $this->_pageForPagination($key)));
      return $this->paginate('User', array(), array(), $key);
     }

Then use it also in the controller: $ this-> set ('byJoinDate', $ this → _ paginateBy ('random'));

:       echo $paginator- > prev ('prev', array ('model' = > $by, 'class' = > 'back'), null, array ('model' = > $by, 'class' = > 'disabled '));

+2

, HTML, , .

, , , , . .

:

class Post extends appmodel { };

Dummy Models - , ,

class Posts1 extends Post { var $useTable = 'posts'; }
class Posts2 extends Post { var $useTable = 'posts'; }

    function multiview($id = null) {

    $this->paginate['Posts1'] = array(
        'conditions'=>array('Posts1.field'=>0),
        'limit'=>5
    );
    $this->set('posts1', $this->paginate('Posts1'));

    $this->paginate['Posts2'] = array(
        'conditions'=>array('Posts2.field'=>1),
        'limit'=>5
    );
    $this->set('posts2', $this->paginate('Posts2'));
}

Display first paginated data
<?php foreach ($posts1 as $post):   ?>
Do Paginated row display here...
<?php endforeach; ?>

<!-- Shows the page numbers -->
<?php echo $this->Paginator->numbers(array('model'=>'Posts1')); ?>
<!-- Shows the next and previous links -->
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $this->Paginator->counter(); ?>

Display second paginated data

<?php foreach ($posts2 as $post):   ?>
Do Paginated row display here...
<?php endforeach; ?>

<!-- Shows the page numbers -->
<?php echo $this->Paginator->numbers(array('model'=>'Posts2')); ?>
<!-- Shows the next and previous links -->
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $this->Paginator->counter(); ?>
+2

Pagination $controller- > pagination, , . Ajax-based. Ajax.

0

? , - , , ? 3 . 2 . , , - AJAX , matiasf.

, , . " " " "?

0

Source: https://habr.com/ru/post/1733754/


All Articles